The Builder is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.
Video Builder pattern
Overview
The Builder design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems in object-oriented software.
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.
Maps Builder pattern
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 steps of construction process.
Disadvantages
Disadvantages of the Builder pattern include:
- Requires creating a separate ConcreteBuilder for each different type of product.
- Requires the builder classes to be mutable.
- Data members of class aren't guaranteed to be initialized.
- Dependency injection may be less supported.
Structure
UML class and sequence diagram
In the above UML class diagram, 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 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.
Pseudocode
We have a Car class. The problem is that a car has many options. The combination of each option would lead to a huge list of constructors for this class. So we will create a builder class, CarBuilder. We will send to the CarBuilder each car option step by step and then construct the final car with the right options:
class Car is Can have GPS, trip computer and various numbers of seats. Can be a city car, a sports car, or a cabriolet. class CarBuilder is method getResult() is output: a Car with the right options Construct and return the car. method setSeats(number) is input: the number of seats the car may have. Tell the builder the number of seats. method setCityCar() is Make the builder remember that the car is a city car. method setCabriolet() is Make the builder remember that the car is a cabriolet. method setSportsCar() is Make the builder remember that the car is a sports car. method setTripComputer() is Make the builder remember that the car has a trip computer. method unsetTripComputer() is Make the builder remember that the car does not have a trip computer. method setGPS() is Make the builder remember that the car has a global positioning system. method unsetGPS() is Make the builder remember that the car does not have a global positioning system. Construct a CarBuilder called carBuilder carBuilder.setSeats(2) carBuilder.setSportsCar() carBuilder.setTripComputer() carBuilder.unsetGPS() car := carBuilder.getResult()
Of course one could dispense with Builder and just do this:
car = new Car(); car.seats = 2; car.type = CarType.SportsCar; car.setTripComputer(); car.unsetGPS(); car.isValid();
So this indicates that the Builder pattern is more than just a means to limit constructor proliferation. It removes what could be a complex building process from being the responsibility of the user of the object that is built. It also allows for inserting new implementations of how an object is built without disturbing the client code.
Examples
C#
The Director assembles a car instance in the example above, delegating the construction to a separate builder object that it has been given to the Director by the Client.
C++
Crystal
F#
In F# we can enforce that a function or method cannot be called with specific data. The example shows a simple way to do it that doesn't fully block a caller from going around it. This way just makes it obvious when someone is doing so in a code base.
Java
Scala
Python
See also
- Currying
References
External links
- Builder pattern implementation in Java
- The JavaWorld article Build user interfaces without getters and setters (Allen Holub) shows the complete Java source code for a Builder.
- Item 2: Consider a builder by Joshua Bloch
Source of article : Wikipedia