Overview Of Builder Design Pattern

Arshad Suraj
Geek Culture
Published in
4 min readMay 28, 2021
Image from — google

What is Builder Design Pattern?

The Builder is a design pattern that allows us to build a complex object from simple objects in a step-by-step manner. The builder design pattern is classified as a creational pattern.

Why Should we use Builder design pattern?

Let’s look at the example below to find the answer to the above question.

Assume that a shop sells hot-buns to their customers. A hot-bun can be either chicken-bun or beef-bun. There are three sizes to choose from: small, medium, and large. A customer can also request extra sauce, cheese or a cool drink when ordering a hot-bun. However, these extra items are optional. Shop will provide it only if the customer specifically requested for it.

For example, If a customer orders a small chicken-bun with extra cheese, he will not receive any additional sauce or a cool drink. With that hot-bun, he’ll only get extra cheese.

So, how do we implement this in a programming language?. Let’s code in JAVA. If we implement this in the traditional manner, we have two options,

  1. Using setters.
  2. Using multiple constructors.

However, both approaches have multiple drawbacks.

Problem with using Setters

Code snippet of using setters
Code snippet for Drinks class
Code snippet for Sauce class

Have a look at the code above. We can use setters, as shown, but the problem is if we use setters we cannot guarantee immutability to the object. In other words, we cannot create immutable objects if we use setters.

what is an immutable object?

An object is considered an immutable object If its state cannot be modified after it has been created. It will behave in the same manner throughout its entire life.

If we used setters as demonstrated above, Anyone can change the values of the variables using the same setters after we created the object.

Therefore this approach is not suitable for creating immutable objects.

Problem with using multiple constructors

Even though we can create immutable objects by utilizing constructors without implementing the setters, still there are drawbacks. see the code below,

A constructor with multiple parameter

If you use the above constructor, you should pass 3 ‘null’ in the parameter if the customer does not request any extra items. Just assume that if our class has more than ten optional variables and if our customer does not order any additional items, we should pass a bunch of ‘nulls.’ right?

Passing a lot of ‘null’ values as parameter to a constructor is not a good practice. It complicates our code and makes it difficult to work with.

To prevent passing a bunch of null values, we can use multiple constructors, one for each combination. However, there is a problem with the number of constructors once again. Just count the number of combinations we have and the number of constructors we’ll need to build.

extra cheese|extra sauce|cool drink|   conxtructor
no | no | no ---> Hotbun();
no | no | yes ---> Hotbun(extraDrink);
no | yes | no ---> Hotbun(extraSauce)
no | yes | yes ---> Hotbun(extraSauce,extraDrink);
yes | no | no ---> Hotbun(extraCheese);
yes | no | yes ---> Hotbun(extraCheese,extraDrink);
yes | yes | no ---> Hotbun(extraCheese,extraSauce);
yes | yes | yes --->
Hotbun(extraCheese,extraSauce,extraDrink);

Since we had 3 optional variables, we had 2³=8 constructors. just assume if we had 10 optional variables in our class we will have 2¹⁰=1024 constructors 😄.

Therefore, having too many constructors also a bad design.

Note: As a result, to deal with these problems, The Builder design pattern is introduced. We can create immutable complicated objects without many parameters using the builder design pattern. Furthermore, rather than sending a bunch of ‘null’ to parameter, we can pass only the values that are required.

Implementation of Builder Design Pattern

class Diagram of Builder Design Pattern

Let’s have a Look at codes below,

Code snippet for builder design pattern
Code snippet for object creation

see the main method above, no ‘null’ values were passed. instead of using fixed constructor for instance creation, we can dynamically set the values for optional variables which are required.

Things to Keep in Mind

  • To ensure immutability, the HotBun class should not contain setters. We made all of the properties of the HotBun class as ‘final’ in above implementation. As a result, they can’t be changed after the object is created. This ensures immutability.
  • HotBun class should contain an inner static class which is builder class.
  • Inner class (builder class) should contains all the properties that outer class contains.
  • Inner classes should have a method for each property that sets the value for the property and returns the same instance of the inner class.
  • Inner class should contain a build() method which returns the outer class’s instance by calling the outer class’s constructor.
  • Outer class should have a constructor that takes the instance of the inner class (Builder class) as a parameter.

Confused? Don’t worry, you’ll understand this, See the Code Again😜.

When to use the Builder Design pattern?

  • When we need to create a complex object.
  • When numerous parameters or constructors are required to create an object.
  • when we want to create immutable objects.

Advantages of Builder Design Pattern

  • The parameters to the constructor are reduced and are given in highly readable methods.
  • There’s no need to pass ‘null’ to the constructor for optional parameters.
  • We can end up with a complex object that we have customized.
  • Immutable objects can be built.

Limitations

The one and only major disadvantage is that developers must write a lot of code throughout the implementation process 😢. However, once developed, it is incredibly flexible.

Keep Learning ❤️

--

--