BUILDER DESIGN PATTERN

BUILDER DESIGN PATTERN

=======================================

❉ The builder pattern is a creational design pattern that permits building complex objects in a step by step manner until the final step returns the object.

❉ The builder does not depend on any other object.

❉ A director object which only knows what type of object it needs to create will be used to control the construction.

❉ The object construction process must be generic, therefore it will be able to use to instantiate various representations of the same object.

❉ Sometimes, we need to use multiple constructors in our programs.

🎇 Let us consider such a scenario and get deep into the topic.

Assume you have some vehicles and you rent them out. In this case, you need to provide insurance, road assistance, emergency assistance, highway electric pass etc. Depending on those features, you may have to create multiple constructors as there are multiple combinations.

Let us take only 3 features

◾️ Insurance, Road Assistance, Highway

☞ Some people may need Insurance and Road Assistance, but not Highway

☞ Some may need Highway, but not Insurance and Road Assistance

☞ Some may need only Road Assistance

☞ Some may only need Insurance

❉ If you traditionally implement these with constructors, your code will get too complicated because you need to implement multiple constructors.

❉ Some people use Telescopic constructors.

🔭 In Telescopic constructors, you can have multiple constructors and each constructor use other constructors depending on parameters.

❉ Some people use setters to set the state of an object.

🔧 The real problem that occurs when using setters is that you cannot make it immutable because anyone can change the object after creation.

Ref: https://images.app.goo.gl/ADn5nn35ZFXQhUMb9

UML of Builder Design Pattern

Ref: https://images.app.goo.gl/z2U8SbdM6a49rnBY8

Director: Algorithm which is responsible for generating the final product object will be controlled by Director. A director object is created and the Construct method of it is called.

Builder: This is an abstract base class that specifies the steps to be taken when creating a product clearly. The actual builder functionality is performed in the concrete subclasses. This Builder class can be replaced with a simple interface.

ConcreteBuilder: Concrete classes include the functionality that is used to create a specific product that is complex. There can be many different concrete builder classes that inherit from Builder.

Product: This class specifies the complex object’s type that will be generated by the builder pattern. And also Product will be returned by the GetProduct method of the builder object.

Usage of Builder Design Pattern

❉ Builder design pattern can be used in following situations when;

📍 You need your code to create multiple representations of a product.

📍 You need to create complex objects and composite trees.

📍 You need to guarantee immutability

📍 You need to create objects with different optional parameters.

📍 You do not want to use Telescopic constructors.

Real World Example for Builder Design Pattern

📜 “Pizza Corner” is a restaurant located in Kandy that delivers pizza and other food items according to the orders made by the customers.

There are mainly 3 other items that can be ordered with main meal namely;

▪️ Starters

▪️ Beverages

▪️ Desserts

Some people may need Main meal, starters and desserts, but not beverages. Some may need main meal and beverages, not starters and desserts. Some may need only the main meal.

If you traditionally implement these with constructors, you code will get too complicated because you need to implement multiple constructors.

Some people use telescopic constructors when implementing these type of problems. Let us see how we can implement the above scenario using telescopic constructors.

💫 MealTelescopic1 class

This is one way of implementing the class using telescopic constructors.

There is another way that we can implement the same program using telescopic constructors.

💫 MealTelescopic2 class

💫 Application class

Once you run this program, you get the output as follows.

OUTPUT

Although the problem can be solved, this is not a good practice because it is really complicated. This can be implemented in a smarter way using Builder pattern.

💫 MealOrders class

This is how we implement the above scenario using Builder class.

💫 Application class

The same Application class used above that has the main method is modified to run the MealOrders class using Builder design pattern.

OUTPUT

Advantages of Builder Design Pattern

♻️ Can guarantee immutability without any complicated logic.

♻️ Reduces the number of parameters in the constructor.

♻️ Efficient since we do not need to pass values to the empty parameters in the constructor.

♻️ Provides highly readable method calls.

♻️ Controls Construction process well.

♻️ Helps in changing the objects’ internal representation.

♻️ Construction and representation of an object is differentiated coherently.

♻️ Instantiation of objects in a complete state.

♻️ Flexible design

Disadvantages of Builder Design Pattern

♨️ Initially, you need to do lot of coding depending on the number of parameters you need to implement. But it is very easy to use once you implement it.

♨️ For each different product type, you will be needing a separate ConcreteBuilder.

Real world examples for Builder Design Pattern

🔶 java.lang.StringBuilder#append()

🔶 java.nio.ByteBuffer#put() (also in CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)

🔶 java.lang.StringBuffer#append()

🔶 javax.swing.GroupLayout.Group#addComponent()

Referred from: https://www.adevguide.com/builder-design-pattern-java/#example-of-builder-design-pattern

References

--

--

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