Luca Fülbier Luca Fülbier - 4 months ago 7
Java Question

Implementing the builder pattern

I have seen two popular methods of implementing the builder pattern:

// 1. The build() approach
Product p = builder.part1()
.part2()
.build();

// 2.The constructor approach
builder.part1()
.part2();
Product p = new Product(builder);


Which of these is preferable?

Answer

1st one is the way to go...

if you use the 2nd choice then doing this:

Product p = new Product(builder);

will add dependencies to the Product class..

that means the Product class needs now at least a constructor with the parameter builder

Comments