We build objects

We build objects

01 September 2018

Problem

Imagine a complex object that requires laborious step by step initialization of many fields and nested objects. Such code is usually buried inside a constructor with lots of parameters, or worse, scattered all over the client code. For example, let's think how to create a House object. To build a simple house, you need to construct four walls, install a door and two windows, build a roof. But what if you want a bigger, brighter house, with a backyard and other goodies?

The simplest solution that comes to mind is to extend the base House class and to create subclasses to cover all combinations of the parameters. The obvious problem with this approach is a considerable number of subclasses you will end up with. Any new parameter, such as porch style, will require growing this hierarchy even more.

On the other hand, you can create a giant constructor in the base House class that accepts all possible parameters of resulting house. While this approach will indeed eliminate the need for sublcasses, it will also create another problem. Some of these parameters will be unused most of the time, making constructors calls pretty ugly. For instance, most houses do not have a swimming pool. Therefore, all parameters related to swimming pools will be useless in 99% of cases.

 

Solution

The Builder pattern suggests to extract the object construction code out of its own class and move it to separate objects called builders. The pattern organizes the object construction into a set of steps (such as buildWalls,  buildDoor, etc.) To create an object, you will need to call several building steps in a builder class. The important part here is that you do not need to call all the steps. You can use only the steps necessary for producing a particular configuration of an object. Often, the same construction step will be different for various products that you are trying to build. For example, a cabin may be build with wooden logs, but a castle must be build with stone.

In this case, you can create several different builder classes that implement the same building steps in a different manner. Then you can use these builders in the same construction process to produce different kinds of objects. For example, there is a StoneBuilder class that makes everything from stone and a WoodBuilder class that uses wood. You could pass one of them through a set of construction steps to produce a house built either from stone or wood. This would work only if the client code that calls building steps interacts with builders using their common interface.

You can go even further and extract the whole construction process to a special class called Director. In this case, the Director will be defining the order of building steps, and a Builder object, linked with the Director, will execute them. This structure will hide any construction details from client code.

“ Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla nunc dui, tristique in semper vel, congue sed ligula. Nam dolor ligula, faucibus id sodales in, auctor fringilla libero. ”

Vestibulum sodales ante a purus volutpat euismod. Proin sodales quam nec ante sollicitudin lacinia. Ut egestas bibendum tempor. Morbi non nibh sit amet ligula blandit ullamcorper in nec risus. Pellentesque fringilla diam faucibus tortor bibendum vulputate. Etiam turpis urna, rhoncus et mattis ut, dapibus eu nunc. Nunc sed aliquet nisi.