Easy patterns: Builder

Ruslan Malogulko
ITNEXT
Published in
3 min readSep 24, 2018

--

This article is created in continuation of easy patterns series description and presents a Builder pattern which solves the problem of separation between construction of complex object and its representation.

Creational patterns:

Simple Factory

Factory method

Builder (this article)

Singleton

Abstract factory

Prototype

Structural patterns:

Adapter

Decorator

Bridge

Composite

Facade

Flyweight

Proxy

Behavioral patterns:

Visitor

Mediator

Observer

Memento

Iterator

Chain Of Responsibility

Strategy

State

The main essence

The builder pattern is needed when the algorithm for creating object should be separated from the part that creates the object itself. Such construction the process which allows different representations for object that is constructed.

For example you want to create some object which has many properties inside. You call its constructor with a long list of parameters that would be assigned as internal properties of newly created object. This is a common example of a problem known as Telescoping Constructor.

This pattern includes three main roles:

  • Builder — constructs parts of the product and provides an interface for its retrieving
  • Director — constructs an object using the Builder interface
  • Product — represents the complex object under construction. This is a result of cooperation a concrete Director with a concrete Builder

Example of use

In this example we are creating an airplanes based on some specifications which we are describing with the help of builders. After creating and updating specific builder, it’s easy to pass it as a parameter to a Director class and instantiate a concrete Product.

Profit

This pattern lets you to vary a product’s internal representation. Each Builder contains all the code to create a particular kind of a Product. The code of a Builder is written once but can be reused by different Directors.

This pattern isolates code of construction and representation. The builder provides a Director with an abstract interface for constructing the product. This interface helps to hide representation and internal structure of the Product.

This pattern gives you a better control over the construction process. Unlike other patterns which are building the Product right away, the Builder pattern constructs the Product step-by-step under the Director’s control.

All this gives you a great control over the construction process of resulting Product.

Weak places

Sometimes you might need access to parts of the product constructed earlier. As far as a Builder constructs its product in step-by-step fashion it’s harder to give such access. At some point you need to return already created child nodes to the Director which will supply access for the Builder in further steps.

Conclusion

There is a similar pattern to construct complex objects named an Abstract Factory. But the main difference is that Abstract Factory constructs the families of product object and does this in one shot (returns a Product immediately). From the other hand the Builder pattern helps to build a complex object in step-by-step way and returns the Product as a final step.

If you found this article helpful, please hit the 👏 button and feel free to comment below!

--

--