> Design Patterns > Builder Design Pattern

Builder Design Pattern

Builder design pattern is used to create complex object in steps with different representations.

Intent

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.

The intent of builder design pattern is to separate the construction process of complex object and its different representation uses the same construction process.

Problem

  • Construction of complex object which requires different representations.

Sometime complex object requires to be created in steps and then comes the final object, so it's like creating component and assembling it into final product. Also sometime it is required to have different representation, so different components are required for assembling of a particular product.

So having the construction and representation together will make it complex and will be difficult to modify and extend in future.

Solution

  • Come up with way to create complex object in steps.
  • Construction process should be separated from representation of object.
  • Provide the way for different representation of object.

Where builder design pattern is applicable?

  • A complex object has to be created in steps.
  • Same construction process of object is required to provide different representation of final object.
  • The construction process requires separation from representation.

Builder Design Pattern UML Structure

Builder Design Pattern UML Structure

Participant classes of builder design pattern

  • Builder provides interface for creating parts of object.
  • ConcreteBuilder class is derived from Builder and implements the methods of Builder for creating parts of object. This assembles the part to make a final product. It has the representation of objects and provides the interface to get the created object.
  • Director class has Builder as aggregation, it uses builder interface for constructing the object.
  • Product represents object to be created.

How they work together?

  • Client uses the Director and provides the information for the product it wants to create. Director has construction logic based on the provided information, and uses builder interface to create parts of object, concrete builder methods create the part of the object and assemble them for a required product. The builder provides the product to Director through its interface.
  • So you can see construction process is separated from representation of object. The internal structure of object is not known to the outside world as it is with concrete builder only.
  • The construction process has a way to create object in parts and well managed by Director.
  • New representation of object will require adding new concrete builder which can be easily added. Also it will be easy to modify the construction logic and representation of objects.
  • Some scenarios may require access of intermediate object and has to be taken care through builder. You can see, there is no interface for product as final objects will be quite different and also well known to client as it provides the input.
  • Builder has all the methods for creating parts of objects, so the required ones have to be implemented in concrete builder.

Builder Design Pattern Example

Here is the builder design pattern example.

We want to create System Manager for Unisys and IBM System.

Builder Design Pattern Example

  • SystemManagerCreator class gets the input as information of system from client. It has instance of abstract class SystemManagerBuilder. It uses the appropriate interface of SystemManagerBuilder class to create the parts of object it requires through logic in Construct() method.
  • The concrete class UnisysSystemManagerBuilder builds the parts of object for UnisysSystemManager through implementation of method BuildPartA() and BuildPartB(); and assembles it as object of UnisysSystemManager class. Similarly, the concrete class IBMSystemManagerBuilder builds the parts of object for IBMSystemManager through implementation of method BuildPartC() and BuildPartD(); and assembles it as object of IBMSystemManager class.
  • SystemManagerCreator gets the final object through interface GetSystemManager() at the end of construction process in Construct() method and provides it to client.

Implementation Code

C++ Builder Pattern in C++ Builder Pattern C++ Example
C# Builder Pattern in C# Builder Pattern C# Example
Java Builder Pattern in Java Builder Pattern Java Example
Python Builder Pattern in Python Builder Pattern Python Example
JavaScript Builder Pattern in JavaScript Builder Pattern JavaScript Example
PHP Builder Pattern in PHP Builder Pattern PHP Example
Ruby Builder Pattern in Ruby Builder Pattern Ruby Example
Swift Builder Pattern in Swift Builder Pattern Swift Example
Objective-C Builder Pattern in Objective-C Builder Pattern Objective-C Example


Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books C In Depth, Data Structures Through C In Depth. He has 18+ years experience in industry and worked on architecture and design of multiple products. This article Abstract Factory Design Pattern is from his Design Patterns course.