Builder Design Pattern

Introduction

Ernest Lim
3 min readSep 21, 2021

The builder design pattern allows for complicated objects to be constructed in a simplified manner. The builder pattern is particularly useful when an object can have many representations, and the construction process of these representations are similar. By using the builder pattern, it allows for the construction and the representation of an object to be separated.

How does the builder class work?

A Builder class is created, which organizes the construction process of the object. The builder class contains the class of the object it builds, the methods required to build a part of the object and a method to return the constructed object. To construct a representation, the methods that are required by that representation are called.

Director class

When following the builder design pattern, it can be helpful to create a Director class. A Director class contains a Builder object, and it has methods which specify the order that the builder methods are being executed. These methods returns the class of the object that is being built. The Director class allows abstraction of the construction process, which can be reused in different parts of the code. The Director class also allows the client to create objects without knowing the full process of how the object is being built. The client only requires the information of which builder to use to construct the object, simplifying the object creation process for the client.

Class Diagram

The class diagram above illustrates an example of the builder pattern in use. In the class diagram, the class that is being built is Room. The Builder interface specifies the methods to build parts of the Room object, and the child classes RoomBuilderTypeA and RoomBuilderTypeB are concrete Builder classes that specify the implementation for building those parts. The Director class uses the objects of the Builder class to create a Room object. For example, makeBasicRoom might only call builder methods buildDoor, while makeFancyRoom calls all three builder methods.

What are the benefits of using the Builder Design Pattern?

Contrasting to a traditional approach where a class requires multiple constructors that take in varying number of parameters, the builder approach simplifies the construction process. It also increases readibility, as it becomes clear which attributes are being constructed when method names are used. The builder design pattern encapsulates the code construction and representation, which allows for information hiding of the construction process to non-builder classes.

Drawbacks of using the Builder Design Pattern

Since the builder object relies on mutation of state to create the object, if the builder is not properly reset after an object is created, it might result in a new object having parts of the previous object. This drawback can be overcome by using the Factory pattern to instantiate the Builder objects.

Applications of the Builder Design Pattern

The class diagram above outlines a hypothetical use of the Builder Design Pattern. A real-life example of the Builder Design Pattern is in CS2103’s AddressBook Level-3 program. The Builder Design pattern is used in the unit tests to allow the objects to be built with only certain attributes defined, which is especially helpful in negative test cases, where some attributes are left as undefined.

--

--