my-server
← Wiki

Builder pattern

The builder pattern is a design pattern that provides a flexible solution to various object creation problems in object-oriented programming. 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 and is sub-categorized as a creational pattern.

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 <code>Builder</code> object.
  • A class delegates object creation to a <code>Builder</code> object instead of creating the objects directly.

A class (the same construction process) can delegate to different <code>Builder</code> 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 errors for unset fields to runtime.

Structure

UML class and sequence diagram

In the above UML class diagram, the <code>Director</code> class doesn't create and assemble the <code>ProductA1</code> and <code>ProductB1</code> objects directly. Instead, the <code>Director</code> refers to the <code>Builder</code> interface for building (creating and assembling) the parts of a complex object, which makes the <code>Director</code> independent of which concrete classes are instantiated (which representation is created). The <code>Builder1</code> class implements the <code>Builder</code> interface by creating and assembling the <code>ProductA1</code> and <code>ProductB1</code> objects. <br/> The UML sequence diagram shows the run-time interactions: The <code>Director</code> object calls <code>buildPartA()</code> on the <code>Builder1</code> object, which creates and assembles the <code>ProductA1</code> object. Thereafter, the <code>Director</code> calls <code>buildPartB()</code> on <code>Builder1</code>, which creates and assembles the <code>ProductB1</code> 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:

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

Notes

References

External links