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?
-
How can a class (the same construction process)
- 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 separateBuilderobject. -
A class can create different representations
of a complex object by delegating to
different
Builderobjects.
-
Separate the
construction of a complex object from its
representation
-
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
Builderobjects:-
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.
-
Define an interface
for creating parts of a complex object
(
-
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 complex object to aBuilderobject (builder.buildPartA(),builder.buildPartB(),…). -
This enables run-time flexibility (via object composition).
A class (the same construction process) can use differentBuilderobjects 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.
-
Creating a representation of a complex object
is implemented (hard-wired) directly within a
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 (encapsulated)
in a separate class
(
Builder1). -
This makes it easy to create a different
representation
independently from
(without having to change)
clients (
Director).
-
Creating a representation of a complex object
is implemented (encapsulated)
in a separate class
(
-
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?
-
How can a class (the same construction process)
Refactoring Problems
-
Complicated Code
-
How can a class that includes
creating
a complex object
be simplified?
Encapsulate Composite with Builder (96) [JK05]
-
How can a class that includes
creating
a complex object
be simplified?
Structure
Static Class Structure
-
Director-
Refers to the
Builderinterface to create parts of a complex object. - Is independent of how the complex object is created (which concrete classes are instantiated, i.e., which representation is created).
-
Maintains a reference
(
builder) to aBuilderobject.
-
Refers to the
-
Builder- Defines an interface for creating parts of a complex object.
-
Builder1,…-
Implement the
Builderinterface by creating and assembling the parts of a complex object.
-
Implement the
Dynamic Object Collaboration
-
In this sample scenario,
a
Directorobject delegates creating and assembling the parts of a complex object to aBuilder1object.
Let's assume that theDirectoris configured with aBuilder1object. -
The interaction starts with the
Directorthat callsbuildPartA()on the installedBuilder1object. -
Builder1creates aProductA1object and adds it to theComplexObject. -
Thereafter, the
DirectorcallsbuildPartB()onBuilder1. -
Builder1creates aProductB1object and adds it to theComplexObject. -
The
Directorcan then get the assembledComplexObjectfrom theBuilder1and 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.