Understanding Builder Design Pattern

In my previous article, we have discussed about Singleton Design Pattern.

In this article, we will discuss about another Creational Design Pattern i.e. Builder Design Pattern.

I got a call from my colleague “Mukesh”

Mukesh: Let’s have a party
Me: Why?
Mukesh: It’s just “Team Lunch”
Me: Team Lunch? Who else is coming?
Mukesh: Rawat and Agarwal will join us
Agarwal: I have some work I can’t go far
Rawat : Let’s go out but near by
Me: Let’s go to “Spice Garden”

At last we(I, Agarwal, Rawat, Mukesh) have shortlisted to meet at Spice Garden for Team Launch

As of now we have following requirements for Party

Requirements:

  1. The place for the Party
  2. The purpose(name) for the Party?

Let’s create a Party class with two variables name and place. Following is the code snippet.

We are at Spice Garden

Rawat: What are services available here?
Hotel Manager: We have different prices for each service. Pay as you opt for service. Following are the services available-
  1. Food starter (e.g. Potato Chips, Chicken Lollipop)
  2. Main course (Chicken Biriyani, Noodles)
  3. Dessert (ice-cream)
  4. Wide range of dance types (classical, pop, party special)

Now lets update the Party class to avail all these services.

Mukesh: How to use the above Party class for invoice?
Me: Like following
Out Put:
Mukesh: Its great, As I am moving to a new team, Can you use this above code for Party invoice in that team?
Me: Sure, Take this

After Few days

Mukesh: I found few Problems while using the above code (Party class),
Me: What?
Mukesh:
  1. I have to remember all the items for Party at the time of sending Party event.
  2. I have to remember the exact sequence of items
  3. I want to send Meeting invite, in that case I don’t have any interest in starter, main course food, dessert etc. I have to specify the unnecessary items as null.
  4. I want to have flexibility to update the items any number of time before setting up Party
  5. I want to ensure that the item can’t be updated after sending invoice (creating Party object). (Restriction on item updation after Object Creation)
  6. I want to make name and place as mandatory for all type of Party event. (Custom Restriction on object creation)

My first approach to solve Problem-3 (remove passing Null value for Unnecessary items)

Lets create different constructors

Problem in above approach

How many constructors I need to create at worst case?

For 5 parameters, in worst case I need 2⁵=32 constructors

How to match all of above criteria?

How to Solve Problem- 1, 2, 3,4?

Instead of creating so many constructors, it is better to create setter methods for each variable (item). At max I can have only 5 methods for 5 variables

How To Solve Problem-5?

Step-1 : Create a class (PartyBuilder) to delegate the item updation and Object Creation

Step-2: To restrict Object creation (party) from outside :-

  1. Make the Party class constructor as private
  2. Update above PartyBuilder class as static inner class (so that the builder class can be access by outside)

Now the complete source code looks like following

Me: Mukesh, Use this new Party class for your Invoices.
Mukesh: How can I use this class?
Me: Here is Sample

Class Diagram

Intent:

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

When to use?

the algorithm for creating a complex object should be independent of the parts that make up the object and how they’re assembled the construction process must allow different representations for the object that’s constructed

What does Builder Pattern do?

Builder pattern builds a complex object using simple objects and using a step by step approach. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. A Builder class builds the final object step by step. This builder is independent of other objects.

Type

As the Builder pattern involves in Object creation It comes under Creational Design Pattern.

Example : StringBuilder, AlertBuilder, NotificationBuilder

Source Code:

Github