Builder Pattern: Why does the director construct the object?

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.

/** "Director" */ 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(); // construct component 1 pizzaBuilder.buildSauce(); // construct component 2 pizzaBuilder.buildTopping(); // construct component 3 } 

}

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 .. :(

+6
source share
4 answers

The director does not create the object, Builder does, and he named the Product. A director exists for one reason: so that class users do not know anything about the steps required to create an object. All this talk about simply smoothing it all into one method is reminiscent of the question: why can't we make a car in a unicycle. We could only a. it will no longer be a machine, and b. he will not be able to do everything that cars that bikes cannot do do.

One of the best ways to look at Builder is to fuse the Template Method and Factory: we need an abstract building process, so we can support the creation of different products, but the building process involves many steps. The idea is that when you have the Director / Builder architecture, you can easily add new collectors by providing a new concrete implementation. This is similar to the template method, because Builder can also ignore details of the construction semantics and simply deliver implementations of each required part of the structure (note also that some logic may appear in the abstract base of the Builder hierarchy, which further extends the scope from the automatic functionality available for subsequent expanders )

+6
source

I think you misunderstood the Builder pattern. You say that the process of constructing various components can be combined into one single method, and this is true, but it is not the key to this design pattern of an object. Take a look at this code again:

 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(); pizzaBuilder.buildSauce(); pizzaBuilder.buildTopping(); } } 

Please note that Waiter only works with PizzaBuilder , he does not know whether it is SpicyPizzaBuilder or HawaiianPizzaBuilder , and the purpose of this template. In this other piece of code:

 class BuilderExample { public static void main(String[] args) { Waiter waiter = new Waiter(); PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder(); PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder(); waiter.setPizzaBuilder( hawaiianPizzaBuilder ); waiter.constructPizza(); Pizza pizza = waiter.getPizza(); waiter.setPizzaBuilder( spicyPizzaBuilder ); waiter.constructPizza(); Pizza anotherPizza = waiter.getPizza(); } } 

you can see how it works. A Waiter is created and transferred first to a HawaiianPizzaBuilder , and then to a SpicyPizzaBuilder , and with the same instance of Waiter you can build a pizza and ask for it, and it will put the correct one. I hope I explained to myself that this comment helps you;) Good luck !!!

+5
source

The goal of the Director class is to encapsulate an algorithm to create an object, i.e. separate the construction logic code from the code for the parts that actually make up the object. Without the Director class, the Builder class would be more bulky and less modular, so it would be harder to maintain and less reusable.

The Wikipedia example you are referring to is quite simplified. With complex objects, the construction mechanism is usually much more active, and the Director does not necessarily invoke the Builder methods one after another and in the order in which they are defined. Consider, for example, the Cake class: it can have ChocolateLayer , CreamLayer and Strawberry parts. The builder would define three methods for constructing these three parts in this particular order, however the constructCake() method in your Director class might look something like this:

 public void constructCake() { CakeBuilder.createNewCakeProduct(); CakeBuilder.buildChocolateLayer(); CakeBuilder.buildCreamLayer(); CakeBuilder.buildChocolateLayer(); // 2nd call of the same Builder method int totalStrawberries = 3 + new Random().nextInt(2); for (int i = 1; i <= totalStrawberries; i++) CakeBuilder.buildStrawberry(); // put some strawberries on top // Please don't try this recipe at home! } 

Regarding the differences between Builder and Decorator, you can check out the Builder Vs Decorator pattern .

+4
source

My explanation of the Builder pattern: suppose we have an Object (Product), and to build an object, many parameters or components (other objects) are required, now to make things worse, not all of these parameters could be required, this will lead us to either create many designers with different parameters, or by introducing many logical operators inside a single constructor in order to select this and reject this object and make it even worse, it is possible that these components need to be built in order (in a specific sequence) or Overko some business rules to them, it all boils down to the complex constructor, which is closely associated with parameters (components) and difficult to maintain. The solution is to move this logic to a specific builder so that we get our object by calling ConcreteBuilder.BuildComponent1(), ConcreteBuilder.BuildComponent2(), ..., ConcreteBuilder.GetMeMyObject() , but this will closely associate the client code with by the construction process, each client is now fully aware of all the steps necessary to build our favorite object, so any change in the construction process will force us to change the entire client code here and there to solve this problem. In the Builder template, a Director is introduced that encapsulates these steps, and now all clients call the Build () method the director for the assembly object. To summarize, the Director is there, so the client code does not know or is not closely related to the steps necessary to create our favorite object, now why are there the methods BuildComponent1(), BuildComponent2() , well, they are there to solve problems the linker template is and for which I mentioned at the beginning: now we can ensure order in the process of building the component, we can also ensure compliance with business logic that is unique to each specific builder within these methods, we can even refuse to implement an empty for concreteness of specific builders, because it is simply our final object (product) does not need this component, and we are now encapsulated the complex process of construction of our favorite object that does the job better, without having to worry about how to build it.

+1
source

All Articles