Design patterns #2: The Builder Pattern and the Telescoping Constructor anti-pattern

In the previous article in this series we had an introduction to the concept of design patterns. As of this one, we'll start studying some popular design patterns implementations as well as the scenarios where they are useful.

As you can see, today's case of study are the Telescoping Constructor anti-pattern and the Builder pattern. Let's get started.

Telescoping Constructor anti-pattern

First of all, let's take a look at the problem so we can have a better understanding of the solution latter.

The Telescoping Constructor is an example of what we know from the previous chapter as an anti-pattern. Regrettably, this anti-pattern is too often used in used in projects even though there are better solutions available. In this (anti)pattern, your class has numerous constructors each one taking a different number of parameters, so you can instantiate that class with the correct combination of parameters on each situation, but that at the end, if the class has been properly written, all this constructors delegate to a default constructor.

So what is the problem with that?

The problem with this (anti)pattern is that once your class has to much constructors and constructors are 4 or more parameters long it becomes difficult to remember the required order of the parameters as well as what particular constructor you might want in a given situation. Giving yourself and your fellow developers a hard time trying to initialize that class.

The Builder pattern

The Builder pattern is an object creation design pattern which intent is to separate the construction of a complex object from its representation. By doing so, the same construction process can lead to different representations.

The Builder pattern deffer from the Abstract Factory pattern and the Factory Method pattern, in that the intention of those two is to enable polymorphism, while the intention of the builder pattern is to find a solution to the Telescoping Constructor anti-pattern. To solve the telescoping constructor problem, instead of using numerous constructors, the builder pattern uses a builder, that is another object that receives each initialization parameter step by step and then returns the resulting constructed object at once.

The builder pattern can also be used for objects that contain flat data (html code, SQL query, X.509 certificate…). This kind of data cannot be edited step by step and must be edited at once. So, the best way to construct such an object is to use a builder class. Builders are good candidates for a fluent interface.

Advantages

We can state that the following are benefits of the Builder pattern:

  • Allows you to vary a product’s internal representation.
  • Encapsulates code for construction and representation.
  • Provides control over steps of construction process.

Disadvantages

The main pitfall of the Builder pattern is that requires creating a separate ConcreteBuilder for each different type of Product.

From Telescoping Constructor anti-pattern to Builder pattern

Bellow there's an example of class that make use of that Telescoping Constructor anti-pattern:

As you can see, this list of constructors and their parameters can grow as far as the number of pizza ingredients. An enormous set of constructors can add complexity when trying to initialize the class with the correct parameters.

Now let's see how we can solve this issue with the Builder pattern:

As you can we delegate the construction of the Pizza object to an inner class named Builder, making much easier and intuitive the initialization of a Pizza object. The Builder class doesn’t have to be inside the class it will construct, this is just an style.

Now you can cook a pizza like this:

Conclusion

In this second chapter we’ve learn more about design patterns, specifically the Builder pattern and the Telescoping Constructor anti-pattern. We’ve seen through example how messy the Telescoping Constructor can become and how easy is to solve this problem implementing a Builder class. Hope you’ve found this article helpful and that you are willing to read the next one about the Factory Method and Abstract Factory pattern.

Follow me on medium and stay up-to-date on this and other article series.
Also remember follow me on twitter, twitter.com/@modestofiguereo.

Happy coding ❤

Design patterns #3: Factories

Written by

Tech Geek, Life Hacker and Dancer. 🎶 Salsa Casino, LA Style, NY Style, Kizomba, Bachata 🎶

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store