The Builder Pattern is a type of design pattern template that is used to solve programming tasks in object-oriented programming. Builder (or Constructor) patterns make the programming process easier for developers because they don’t have to redesign every repeating step as a program routine.
Instead of redesigning each step, they can use an established solution. The software elements are based on the book Design Pattern: Elements of Reusable Object-Oriented Software published in 1994 by four American software developers known as the Gang of Four , or GoF for short. In this guide we introduce you to the main aspects of the Builder design pattern and include a practical example.
The Builder Pattern in detail
The Builder Pattern is part of the construction pattern group of design patterns. It improves both the construction security and the readability of the program code. The goal of the Builder design pattern is to create an object without known constructors but with a helper class.
“Separate the construction of a complex object from its representation so that the same construction process can create different representations.”
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (Gang of Four)
Source: Design Patterns: Elements of Reusable Object-Oriented Software , 1st Edition, Ed. Addison-Wesley Professional (November 10, 1994).
A Builder design pattern distinguishes between four actors :
Director : this actor builds the complex object with the constructor interface. You are aware of the builder’s sequencing requirements. With the director, the construction of the object is disassociated from the client.
Builder: provides an interface to create the components of a complex object (or product).
Specific builder : creates the parts of the complex object , defines (and manages) the representation of the object, and maintains the output interface of the object.
Product : is the result of the “activity” of the Builder Pattern, that is, the object that is built.
The manager oversees the decisive process of the Builder pattern: the separation of the creation of an object / product from the customer.