I am learning a builder pattern
In the link above (Java example), I noticed that Builder offers an interface for creating multiple components. Along with calling them, we also call getProduct ().
The point I donโt understand is why the director must call all these component layout methods every time and get the result at the end.
class Waiter { private PizzaBuilder pizzaBuilder; public void setPizzaBuilder(PizzaBuilder pb) { pizzaBuilder = pb; } public Pizza getPizza() { return pizzaBuilder.getPizza(); } public void constructPizza() { pizzaBuilder.createNewPizzaProduct(); pizzaBuilder.buildDough();
}
Why don't we include the code for constructing components 1, 2, 3 in the ConcreteBuilder class, and not in the Director, but actually remove the Director layer.
I understand that the above approach can turn the Builder template into something else, but I donโt understand why the director does the work step by step. What is the use? If there are several directors, there will be a duplicate code, right? Perhaps I do not understand the motive behind the implementation of the Builder pattern ...
UPDATE : Does the Builder template focus on choosing a custom component when creating a more complex object? Otherwise, at the moment, I see no reason to introduce an additional level, director.
Even if in this case, the Decorator template might be a better idea to do the same thing by dynamically tuning components. Somewhere I miss the point for the Builder .. :(
source share