Builder Pattern

The Builder pattern separates the construction of a complex object from its representation so the same construction process can create different objects. 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. This simplifies the creation of complex objects by defining a class that builds instances of another class. The Builder pattern produces one main product, and there might be more than one class in the product, but there is always one main class. The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects. Often, the Builder Pattern is used to build Products in accordance to the Composite pattern, a structural pattern.  When you use the Builder pattern, you create the complex objects one step at a time. Other patterns build the object in a single step.

UML Representation

Builder: Abstract interface for creating objects (product).
Concrete Builder: Provides implementation for Builder. It is an object able to construct other objects. Constructs and assembles parts to build the objects.
Director: The Director class is responsible for managing the correct sequence of object creation. It receives a Concrete Builder as a parameter and executes the necessary operations on it.
Product: The final object that will be created by the Director using Builder.


The following lists the benefits of using the Builder pattern:
  • Lets you vary a product’s internal representation
  • Isolates code for construction and representation
  • Gives you greater control over the construction process

When to Use

You should use the Builder pattern when
  • The algorithm for creating a complex object should be independent of both the parts that make up the object and how these parts are assembled.
  • The construction process must allow different representations of the constructed object.
Subpages (1): Builder Pattern Example