The builder pattern is a
design pattern
A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" ...
that provides a flexible solution to various object creation problems in
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
. The builder pattern
separates the construction of a complex object from its representation. It is one of the 23 classic design patterns described in the book ''
Design Patterns
''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a fore ...
'' and is sub-categorized as a
creational pattern
In software engineering, creational design patterns are design patterns that deal with object creation
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not ex ...
.
Overview
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 that includes creating a complex object be simplified?
Creating and assembling the parts of a complex object directly within a class is inflexible. It commits the class to creating a particular representation of the complex object and makes it impossible to change the representation later independently from (without having to change) the class.
The builder design pattern describes how to solve such problems:
* 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 creating the objects directly.
A class (the same construction process) can delegate to different
Builder
objects to create different representations of a complex object.
Definition
The intent of the builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations.
Advantages
Advantages of the builder pattern include:
* Allows you to vary a product's internal representation.
* Encapsulates code for construction and representation.
* Provides control over the steps of the construction process.
Disadvantages
Disadvantages of the builder pattern include:
* A distinct ConcreteBuilder must be created for each type of product.
* Builder classes must be mutable.
* May hamper/complicate dependency injection.
* In many
null-safe languages, the builder pattern defers
compile-time
In computer science, compile time (or compile-time) describes the time window during which a language's statements are converted into binary instructions for the processor to execute. The term is used as an adjective to describe concepts relat ...
errors for unset fields to
runtime.
Structure
UML class and sequence diagram
In the above
UML class diagram
In software engineering,
a class diagram
in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, operations (or methods), and the re ...
,
the
Director
class doesn't create and assemble the
ProductA1
and
ProductB1
objects directly.
Instead, the
Director
refers to the
Builder
interface for building (creating and assembling) the parts of a complex object,
which makes the
Director
independent of which concrete classes are instantiated (which representation is created).
The
Builder1
class implements the
Builder
interface by creating and assembling the
ProductA1
and
ProductB1
objects.
The
UML sequence diagram
In software engineering, a sequence diagram
shows process interactions arranged in time sequence. This diagram depicts the processes and objects involved and the sequence of messages exchanged as needed to carry out the functionality. Sequence ...
shows the run-time interactions:
The
Director
object calls
buildPartA()
on the
Builder1
object, which creates and assembles the
ProductA1
object.
Thereafter,
the
Director
calls
buildPartB()
on
Builder1
, which creates and assembles the
ProductB1
object.
Class diagram
; Builder
: Abstract interface for creating objects (product).
; ConcreteBuilder
: Provides implementation for Builder. It is an
object able to construct other objects. Constructs and assembles parts to build the objects.
Examples
A
C# example:
///
/// Represents a product created by the builder.
///
public class Bicycle
///
/// The builder abstraction.
///
public interface IBicycleBuilder
///
/// Concrete builder implementation.
///
public class GTBuilder : IBicycleBuilder
///
/// The director.
///
public class MountainBikeBuildDirector
public class Client
A Java example:
public class Employee
public class Main
The Director assembles a bicycle instance in the example above, delegating the construction to a separate builder object that has been given to the Director by the Client.
See also
*
Currying
In mathematics and computer science, currying is the technique of translating a function that takes multiple arguments into a sequence of families of functions, each taking a single argument.
In the prototypical example, one begins with a functi ...
Notes
References
*
External links
{{Auth
Software design patterns
Articles with example Java code