Builder is a creational design pattern for constructing complex objects, it allows a same construction code to create objects with different representations. It is suitable for objects with large number of possible configurations and hard to create an object with many constructor parameters. Unlike other creational patterns, Builder can construct unrelated products, which don’t have common interface, you will see a demonstration in the Example section.
Applicability
- When the object creation is complex, might involve many optional parameters and its cumbersome to provide numerous constructor overloads.
- When you want to create different representations of some objects, the object’s attribute can have different possible configurations(for example, stone, iron, and wooden houses…).
Approach
- Define the product, what structure and attributes it has.
- Create the builder interface, the interface declares the methods for building different parts of the product.
- Implement concrete builders that implement the builder abstraction. Each concrete builder should provide specific implementations for constructing the product.
- Create director that uses a builder to construct the product. The director knows the order in which that parts of the product should be built and orchestrates the construction process.
- Client obtains the product from the director.
Components
- Builder (abstract)
- Specifies an abstract interface that defines a set of methods for creating the product. The methods correspond to the different properties of parts of the product.
- Concrete builder
- Constructs the product or part of the product step by step by implementing the builder methods.
- Keeps track of the representation it creates.
- Provides an interface (getResult) for retrieving the product.
- Director (optional)
- Defines the order in which to execute the building steps, while the builders provide the implementations for those steps.
- Product
- Represents the complex object under construction.
- Concrete builder builds the product’s internal representation and define the process by which it’s assembled.
Example
The builder pattern is used to create instances of Meal
with different configurations.
Identify builder pattern components
Product: Meal
, MealReceipt
Builder: MealBuilder
Concrete builder: KidsMealBuilder
, VeggieMealBuilder
Director: MealDirector
Product
The product is Meal
with different different compositions as attributes.
Here the MealReceipt
is another product, it does not have the same ancestor as a Meal
. They are not related.
Builder
The builder is an interface that defines all common for building different parts of the Meal
product.
Concrete builder
The concrete builder implements the actions defined in the common interface.
In this case we build the meal receipt for a meal, using the same steps as building a meal. This is a feature of builder pattern that it can construct unrelated products that does not belong to a same parent or of a same type.
Director
The MealDirector
is responsible for orchestrating the construction process. It guides the construction with a predefined sequence of steps. The director work with a builder object and may not know what product is being built (meal or receipt) as details are encapsulated.
Client
Everything comes together at the client code.
In real-world implementation, you may use enum class to organise different configuration types instead of using string. enum provides an a concise way of communication and comes with enhanced memory save and performance.
Design_PatternCreational_Design_Patterns SOFT2201Builder_PatternEnum