Intro to Design Patterns: Builder Pattern
Join the DZone community and get the full member experience.
Join For Free
Intent of the Pattern
The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. - Gof
Type
Object Creational
Solution
The Builder Pattern simplifies the construction of complex objects by only specifying the type and content that the object requires. The construction process of the complex object will therefore allow for different representations of the complex object to be created by the different builders. Each of the concrete builder objects will construct a different representation of the complex object.
The Builder Pattern consists of a Builder, ConcreteBuilder, Director and Product.
- The Director object is responsible for the construction process of the complex object but delegates the actual creation and assembly to the Builder interface.
- The Builder object specifies the interface for creating parts of the complex object.
- The Product represents the complex object that is created by the ConcreteBuilder objects. The Product consists of multiple parts that are created separately by the ConcreteBuilder objects.
- The ConcreteBuilder objects create and assemble the parts that make up the Product through the Builder interface.
A client object creates an instance of the Director object and passes it the appropriate Builder object. The Director object invokes the methods on the Builder object to create and initialize specific parts of the Product object. The Builder receives content from the Director object and adds these to the Product object. The Client object has a reference to the Builder object and retrieves the created Product object from it.
Structure
The construction of the Complex object (Product) is hidden by the Builder objects from the Client and Director objects. To change the internal representation of the complex object, a new concrete builder object is defined and used by the client through the Director object. Unlike other creational patterns, the Builder Pattern creates the complex object is sections through the Director and Builder objects. The Builder object may need access to information that was used in previous construction steps. This means that even thought the parts of a Product object is created in individual sections; they may interact with other sections to create the complex product object.
The complex Product objects do not usually have a shared abstract parent object, as their representation differ and a shared parent class or interface might not be possible. As the client object specify the Builder object, it should have the knowledge how to handle the product object that is created by the Builder object.
Java Sample Code
The example for the Builder Pattern is a meal that can be purchased at many fast food franchises. The complex Product is a combo meal that consists of a burger, beverage and a side order. The Builder objects are the different assistants at the till that knows how to create the combo meal for the client. The Director object is the instructions the client gives the assistant on how the specific order should be created.
Example Combo Meal:
Download Combo Meal Example
ComboMealClient.java
The ComboMealClient class makes use of the ComboMealDirector and the
ComboMeal1ConcreteBuilder class to create a complex object called
ComboMealProduct.
Code:
ComboMeal1ConcreteBuilder concreteBuilder = new ComboMeal1ConcreteBuilder();
ComboMealDirector mealDirector = new ComboMealDirector(concreteBuilder);
ComboMealProduct comboMealProduct = null;
mealDirector.constructComboMeal(SuperSize.HUGE);
comboMealProduct = concreteBuilder.getComboMealProduct();
ComboMealDirector.java
The ComboMealDirector class invokes the appropriate methods on the
ConcreteBuilder (ComboMeal1ConcreteBuilder) to create a complex product
ComboMealProduct.
Code:
public void constructComboMeal(SuperSize _mealSize) {
comboMealBuilder.buildBurgerPart();
comboMealBuilder.buildSideOrderPart(_mealSize);
comboMealBuilder.buildBeveragePart(_mealSize);
} // method constructComboMeal
ComboMealProduct.java
The ComboMealProduct class is the complex object whose individual parts is created by the different Builder objects.
ComboMealBuilder.java
The ComboMealBuilder class contains the interface that is used by the ComboMealDirector to create the complex object.
ComboMeal1ConcreteBuilder.java
The ComboMeal1ConcreteBuilder class contains implementation that is used by the ComboMealDirector to create the complex object.
Class Diagram Example
Sequence Diagram
Sequence Diagram by "Yanic Inghelbrecht" with Trace Modeler
References
- Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
Design Patterns: Elements of Reusable Object-Oriented Software. Addison
Wesley, 1995
Published at DZone with permission of Andre Mare. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments