Builder Design Pattern

Builder Design Pattern

In today’s episode of Software Design Patterns you will learn everything about the Builder Design Pattern. The Builder Pattern is also pretty common and widely used. Different from the already covered behavioral pattern Observer, the Builder Pattern is a creational design patterns like Singleton and Factory Method pattern.

All the examples, source code and unit tests are in this GitHub repository.

What’s the purpose of the Builder Design Pattern?

The Gang-of-Four [1] reference book (“Design Patterns. Elements of Reusable Object-Oriented Software“) describes the intent of the Builder Pattern as the following.

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Basically you have a builder and a director. In my example on GitHub I use different types of houses. There is a builder for flat and hipped houses. Those builders are created and passed to the director. The director itself constructs the different houses. To retrieve the built house the actual builder has a method for this (GetHouse()). Via this you can use the concrete house implementation.

How does the Builder look like?

In the picture below you find the Unified Modeling Language (UML) diagram.

Builder Pattern UML
Builder Pattern UML

In the linked GitHub repository you will find interfaces for the IHouseBuilder and the IDirector. Many resources advise you to use abstract base classes for the director and the builder – which is totally fine. I simply like interfaces more, so I went this route. The IHousebuilder interface contains the definition for building the house and retrieving the build instance. The IDirector simply has a construct method. In this method all steps should be done to build a specific thing – in our case a given type of house. The House class stores the model of a house. To make it simple I minimized the house to four property with getter only. To set value, you have to use the constructor when creating a house.

The concrete house builders sets the correspondending values when creating a new instance.

The construct method on the director initiates the build process for the given house builder.

In the unit tests you see the whole process of how to use the builder pattern. In this case it seems pretty odd to use such a pattern, but imagine that the complete build/construct process takes many complex steps.

Appendix: There are many ways of describing the builder pattern. Often it’s mistaken by a fluent library where you chain different methods and eventually build an object from it. Let me know in the comments below how you find the builder or how you learned it.

In the test project you’ll find test cases for subscribing, notifying and unsubscribing from observers.

If you have any questions or comments feel free to leave them below.

That’s it. Happy coding as always :).

References

[1] –Design patterns, software engineering, object-oriented programming

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.