Builder Pattern — Creating complex objects step-by-step
By: Harry Cheng
Tags: Complex Systems, Object Oriented, Builder Pattern, Software Engineering, Gof

When building a house, there are many things to consider. For example, you need to lay out the foundation for the house, construct multiple walls for multiple rooms, electrical wiring for every room, a swimming pool, gym room, you get the idea…

Similarly, when building a codebase, your object constructors can get fairly complex when you are trying to fit everything into a single constructor. It would make more sense to separate out the complex object so that the same construction process can create different representations.

For example, if you try to fit gym rooms in every house that you construct, where only a handful of house owners want gym rooms in their house, the parameter “gym room” would be unused most of the time.

How to solve this problem?

The Builder pattern enforces a step-by-step process to construct a complex object as a finished product. It separates the construction of a complex object from its representation into objects called “builders” so that the same construction can be used to create different representations.

To put into more context, you are able to separate the construction of including a gym room when you want to build a house because some people might want a swimming pool instead. So the process of constructing gym rooms can be reserved for those that need it.

Even within a gym room, you can have multiple variations of it. Some gym rooms can have the choice to build with soundproof walls, mirrored walls or glass walls. You can create several different builder classes that can implement the same building steps but in a different representation. You can then use the same construction process for different materials. Instead of mirrored walls, you can include soundproof walls so you can blast music while you work out.

Because building a house has the same process, constructing code has a similar process as well. This would only work if the client has code that calls the building steps is able to interact with builders using a common interface.

How to visualise?

Typically, the participants of the Builder pattern includes:

Product — Class that represents the product to create Builder — An interface to build parts of a product ConcreteBuilder — Concrete classes that implement Builder to construct and assemble parts of the product and return the finished product. Director — Class that directs a Builder to perform steps in the order required to build the Product.

You can think of Product as the house you want to build, Builder as an interface for your workers that ConcreteBuilder will implement and a Director to manage the steps to the building of your house.

Additionally, having a Director class helps you to hide the details of a product construction from the client code. The client only needs to liaise a builder with a director, execute the construction with the director and get the finished product from the builder.

In this sequence diagram, when a client wants to build a product, it will initialize aConcreteBuilder class which implements from Builder class and aDirector with aConcreteBuilder as a parameter. Then when the client is ready to construct the product, it will call the method Construct() on aDirector class which would instruct the building process by calling BuildPartA(), BuildPartB(), BuildPartC() on aConcreteBuilder. When the construction process is finished, the client can get their finished product by calling GetResult() .

aConcreteBuilder is flexible in a sense where it can implement builders to build particular representations of a product. aDirector class will then guide and gives the order of the construction.

Advantages

Able to construct objects step-by-step Able to reuse same construction code when building different representations of products. Able to isolate the complex construction of code from the business logic of the product.

Disadvantages

Increases overall complexity of code as builder pattern would require creating multiple new classes.