Builder design pattern is used to create complex object in steps with different representations.
Separate the construction of a complex object from its representation so that the same construction process can create
The intent is to separate the construction process of complex object and its different representation uses the same
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
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 it 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 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
Builder has all the methods for creating parts of objects, so the required ones have to be implemented in concrete
We want to create System Manager for Unisys and IBM System.
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.
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 is from his courses on Design Patterns.
CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap
between education and industry and provide the training on specific problems of industry to enhance the skills
of Engineers in industry to do their job appropriately.