Chaper 14 Notes – Builder Pattern Introduction 1. How do you build a house (Product)? 1. You can hire a bunch of contractors (Builders) to do the various tasks (framing, roofing, plumbing, foundation). If you do this you must have knowledge of how to construct a house. For instance, for a slab foundation, you must have a plumber first lay the pipes for the sewage and water and then have a foundation contractor pour the slab. 2. You can hire a general contractor (Director) who will “direct” the construction of your house. He will be responsible for hiring the sub-contractors (Builders) and orchestrating their work. This is the Builder pattern. 2. Use the Builder Pattern to encapsulate the construction of a product and allow it to be constructed in steps. [HFDP, p.614]. In the Builder pattern, we encapsulate the construction of a Product in a Builder class. Then, we have a Director direct the Builders to construct the Product. 3. “The builder pattern encapsulates the logic of how to put together a complex object so that the client just requests a configuration and the builder directs the logic of building it. For example, the main contractor (builder) in building a house knows, given a set of blueprints, how to execute the sequence of operations (i.e. by delegating to subcontractors) needed to build the complex object. If that logic was not encapsulated in a builder, then the buyers would have to organize the subcontracting themselves.” source: source: https://groups.google.com/forum/#!searchin/comp.object/The$20factory$20is$20concerned$20with$20what$20is$20ma de$2C$20the$20builder$20with$20how$20it$20is$20made/comp.object/20s5FN3qUTE/O5iRLm7p5yUJ 4. Separate the construction of a complex object from its representation so that the same construction process can create different representations. [GOF, p.94]. For instance, different sub-contractors may produce the same Component with different representations, e.g. tiled floor vs. hard-wood floors. Or, each client submits a different blueprint to the Contractor and a different House is constructed. 5. The Builder pattern allows a client object to construct a complex object by specifying only its type and content. The client is shielded from the details of the object’s construction. [PIJ, p.129]. For instance, in the example above, the client gives a blueprint for a House to the Contractor and the Contractor has it built. The client is shielded from the details of construction. Non-Software Example 6. In the example below, the Customer (Client) orders a Kid’s Meal, no ketchup, and extra napkins. The Cashier (Director) rings the order up and directs the Workers (Builders) to make a hamburger, fries, and drink. When the Workers are done, the Cashier puts the items on a tray, along with the toy and extra napkins. Source: http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html Difference between Builder and Abstract Factory 7. The Abstract Factory is concerned with what product is made. The Builder pattern is concerned with how a product is made. 8. “The factory pattern defers the choice of what concrete type of object to make until run time. For instance, going to a restaurant to order the special of the day. The waiter is the interface to the factory that takes the abstractor generic message "Get me the special of the day!" and returns the concrete product (i.e Liver souffle or Chicken caramel).” 9. “The factory is concerned with what is made, the builder with how it is made. Design patterns points out that (page 105) Abstract factory is similar to builder in that it too may construct complex objects. The primary difference is that the Builder pattern focuses on constructing a complex object step-bystep. Abstract factory's emphasis is on families of product objects (either simple or complex). Builder returns the product as the final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.” source: https://groups.google.com/forum/#!searchin/comp.object/The$20factory$20is$20concerned$20with$20what$20is$20ma de$2C$20the$20builder$20with$20how$20it$20is$20made/comp.object/20s5FN3qUTE/O5iRLm7p5yUJ 10. Example a. Abstract Factory b. Builder General UML 11. The client requests a Product in a certain configuration from the Director. The Director uses the Builder to construct the component parts in the correct sequence and puts them together correctly. Combining Builder with Other Patterns 12. Builder uses Abstract Factory. 13. Director implements a Template Method References 1. 2. 3. 4. 5. 6. M.Duell – Non-software examples of patterns. Wikipedia – Good description. Data & Object Factory – Vehicle assembly SourceMaking – Converting input into different formats: ASCII, PDF, etc. JavaLobby – Meal director Builder – Panel builder