design-patterns/creational/builder
2020-09-29 22:05:56 +02:00
..
__init__.py doc: create and document packages 2020-09-29 22:05:56 +02:00
Builder.py builder example 2020-09-12 12:56:26 +02:00
builders.py doc: create and document packages 2020-09-29 22:05:56 +02:00
Director.py doc: create and document packages 2020-09-29 22:05:56 +02:00
main.py doc: create and document packages 2020-09-29 22:05:56 +02:00
products.py Singleton (#6) 2020-09-14 21:27:39 +02:00
README.md builder example 2020-09-12 12:56:26 +02:00

Builder

Builder is a creational design pattern, which allows constructing complex objects step by step.

Summary

Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.

Problem

Imagine a complex object that requires laborious, step-by-step initialization of many fields and nested objects. Such initialization code is usually buried inside a monstrous constructor with lots of parameters. Or even worse: scattered all over the client code.

For example, lets think about how to create a House object. To build a simple house, you need to construct four walls and a floor, install a door, fit a pair of windows, and build a roof. But what if you want a bigger, brighter house, with a backyard and other goodies (like a heating system, plumbing, and electrical wiring)?

Solution

The Builder pattern suggests that you extract the object construction code out of its own class and move it to separate objects called builders.

How to Implement

  1. Make sure that you can clearly define the common construction steps for building all available product representations. Otherwise, you wont be able to proceed with implementing the pattern.

  2. Declare these steps in the base builder interface.

  3. Create a concrete builder class for each of the product representations and implement their construction steps. Dont forget about implementing a method for fetching the result of the construction. The reason why this method cant be declared inside the builder interface is that various builders may construct products that dont have a common interface. Therefore, you dont know what would be the return type for such a method. However, if youre dealing with products from a single hierarchy, the fetching method can be safely added to the base interface.

  4. Think about creating a director class. It may encapsulate various ways to construct a product using the same builder object.

  5. The client code creates both the builder and the director objects. Before construction starts, the client must pass a builder object to the director. Usually, the client does this only once, via parameters of the directors constructor. The director uses the builder object in all further construction. Theres an alternative approach, where the builder is passed directly to the construction method of the director.

  6. The construction result can be obtained directly from the director only if all products follow the same interface. Otherwise, the client should fetch the result from the builder.