The 23 GoF design patterns and Dependency Injection – choose a pattern Abstract Factory design patternBuilder design patternFactory Method design patternPrototype design patternSingleton design patternAdapter design patternBridge design patternComposite design patternDecorator design patternFacade design patternFlyweight design patternProxy design patternChain of Responsibility design patternCommand design patternInterpreter design patternIterator design patternMediator design patternMemento design patternObserver design patternState design patternStrategy design patternTemplate Method design patternVisitor design patternDependency Injection design pattern

Builder Design Pattern

Creational pattern. Creational design patterns are concerned with object creation.

Intent: "Separate the construction of a complex object from its representation so that the same construction process can create different representations." [GoF]

Open in the interactive referenceDownload the PDF reference

Intent

The intent of the Builder design pattern is to:
"Separate the construction of a complex object from its representation so that
the same construction process can create different representations."
[GoF]
See Problem and Solution sections for a more structured description of the intent.

  • The Builder design pattern solves problems like:
    • How can a class (the same construction process)
      create different representations of a complex object?
  • Creating and assembling the parts of a complex object directly within a class makes it impossible to create a different representation independently from (without having to change) the class.
  • For example, creating a bill of materials object (BOM).
    It should be possible that a class (the same construction process) can create different product structures (representations) of the BOM.
  • The Builder pattern describes how to solve such problems:
    • Separate the construction of a complex object from its representation -
      encapsulate the creation of a complex object in a separate Builder object.
    • A class can create different representations of a complex object by delegating to different Builder objects.

Problem

The Builder design pattern solves problems like:
How can a class (the same construction process)
create different representations of a complex object?
See Applicability section for all problems Builder can solve. See Solution section for how Builder solves the problems.

  • An inflexible way is to create and assemble the parts of a complex object (new ProductA1(), add to complex object, new ProductB1(), add to complex object, …) directly within a class (Client).
  • This commits (couples) the class to creating a particular representation of the complex object (ProductA1, ProductB1), which makes it impossible to create a different representation (ProductA2, ProductB2, for example) independently from (without having to change) the class.
  • That's the kind of approach to avoid if we want that a class (the same construction process) can create different representation of a complex object.
  • For example, creating a bill of materials object (BOM).
    A bill of materials is organized into a part-whole hierarchy (see also Composite for representing a part-whole hierarchy). It describes the parts that make up a manufactured product and how they are assembled.
    A class should avoid instantiating concrete product classes directly so that it can create different product structures (representations) of the BOM.

Solution

The Builder design pattern provides a solution:
Encapsulate creating and assembling the parts of a complex object in a separate Builder object.
A class delegates object creation to a Builder object
instead of instantiating concrete classes directly.
Describing the Builder design in more detail is the theme of the following sections.
See Applicability section for all problems Builder can solve.

  • The key idea in this pattern is to separate creating and assembling the parts of a complex object from other (construction) code (Director).
  • Define separate Builder objects:
    • Define an interface for creating parts of a complex object (Builder | buildPartA(),buildPartB(),…).
    • Define classes (Builder1,…) that implement the interface.
      The object is created step by step to have finer control over the creation process.
  • This enables compile-time flexibility (via inheritance).
    The way the parts of a complex object are created and assembled can be implemented and changed independently by defining new (sub)classes.
  • A class (Director) delegates the responsibility for creating and assembling the parts of a com­plex object to a Builder object (builder.buildPartA(),builder.buildPartB(),…).
  • This enables run-time flexibility (via object composition).
    A class (the same construction process) can use different Builder objects to create different representations of a complex object.

Motivation

Consider the left design (problem):

  • Hard-wired object creation.
    • Creating a representation of a complex object is implemented (hard-wired) directly within a class (Client).
    • This makes it hard to create a different representation independently from (without having to change) the class.
  • Complicated classes.
    • Classes that include creating a complex object are hard to implement, change, test, and reuse.

Consider the right design (solution):

  • Encapsulated object creation.
    • Creating a representation of a complex object is implemented (encapsu­lated) in a separate class (Builder1).
    • This makes it easy to create a different representation independently from (without having to change) clients (Director).
  • Simplified classes.
    • Classes that delegate creating a complex object are easier to implement, change, test, and reuse.

Applicability

Design Problems

  • Creating Complex Objects
    • How can a class (the same construction process)
      create different representations of a complex object?

Refactoring Problems

  • Complicated Code
    • How can a class that includes creating a complex object be simplified?
      Encapsulate Composite with Builder (96) [JK05]

Structure

Static Class Structure

  • Director
    • Refers to the Builder interface to create parts of a complex object.
    • Is independent of how the complex object is created (which concrete classes are instanti­ated, i.e., which representation is created).
    • Maintains a reference (builder) to a Builder object.
  • Builder
    • Defines an interface for creating parts of a complex object.
  • Builder1,…
    • Implement the Builder interface by creating and assembling the parts of a complex object.

Dynamic Object Collaboration

  • In this sample scenario, a Director object delegates creating and assembling the parts of a complex object to a Builder1 object.
    Let's assume that the Director is configured with a Builder1 object.
  • The interaction starts with the Director that calls buildPartA() on the installed Builder1 object.
  • Builder1 creates a ProductA1 object and adds it to the ComplexObject.
  • Thereafter, the Director calls buildPartB() on Builder1.
  • Builder1 creates a ProductB1 object and adds it to the ComplexObject.
  • The Director can then get the assembled ComplexObject from the Builder1 and use it as required.
  • See also Sample Code / Example 1.

Not included on this page: Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.