Pattern Builder: What is it?

programming

The Builder pattern is part of the design patterns. These are well-tested models that facilitate the work of object-oriented programming, as they allow developers not to have to repeat the repetitive steps every time, giving them the possibility to use an already defined solution. These software elements date back to the 1994 book Design Patterns: Objects for Object-Oriented Software Reuse by the four US software developers known as the “Gang of Four”.

In this article of our guide, we introduce you to the essential aspects of the builder design pattern templates, also with the help of a practical example.

The Pattern Builder in More Detail

The builder is part of the group of creation patterns known as design patterns. It improves both the security during the construction process and the readability of the program code. The goal of the builder models is to allow the construction of an object using a helper class instead of the usual constructors.

In the builder design pattern there are four different players:

  • Director: This actor constructs the complex object using the constructor interface. Knows the manufacturer’s sequence of work requirements. It is at the manager level that the construction of an object is decoupled from the client.
  • Builder: the constructor provides an interface for the construction of the components of a complex object (the product).
  • Concrete Builder: this actor takes care of the actual creation of the parts of the complex object, defining and managing the representation of the object, also having an interface for the object output.
  • Product: the result of the builder pattern activity, i.e. the complex object to build.


However, the decisive step in this pattern occurs at the manager level, where the creation of an object / product is separated from the client.

man front laptop

The Pattern Builder in UML Representation

The modeling language, more commonly called UML (Unified Modeling Language), is used for the graphic representation of programming processes. The graphic below shows how the pattern builder is made up of numerous interacting objects.

The UML Pattern Builder

Basic structure of a UML pattern builder, useful for illustrating the multiple relationships within it. The user of the created object is completely dissociated from this creation.

Pros and Cons of the Pattern Builder

Benefits of the Builder

The construction or creation and the representation (output) are isolated. The internal representations of the constructor are “hidden” by the director. New representations can be easily added thanks to new concrete construction classes. The construction process is explicitly handled by the director. In case you need to apply changes you can do it without having to go through the clients.

Disadvantages of the Builder

Given the close connection between the product, ConcreteBuilder and the classes involved in the construction process, it can be difficult to make changes to the process. Creating objects often requires knowledge of specific applications and their environment. The use of known patterns and the builder can lead programmers not to notice simpler and perhaps even more elegant solutions. The builder pattern is therefore one of the least important design patterns among programmers.

Where is the Builder Pattern Used?

To make the explanation of the builder pattern more intuitive, we can compare it to the simplified dynamics of a restaurant, where a customer places his order. The restaurant staff, which here corresponds to the various actors, acts upon receipt of the order in order to serve what is requested at the table. The whole process up to the arrival of the food ordered by the customer at the table takes place behind the scenes. The customer does not see what happens in the kitchen following his order, but only receives the final result served at the table. In programming language this is commonly referred to as “print”.