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
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
- The place for the Party
- 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-
- Food starter (e.g. Potato Chips, Chicken Lollipop)
- Main course (Chicken Biriyani, Noodles)
- Dessert (ice-cream)
- 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
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),
- I have to remember all the items for Party at the time of sending Party event.
- I have to remember the exact sequence of items
- 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.
- I want to have flexibility to update the items any number of time before setting up Party
- I want to ensure that the item can’t be updated after sending invoice (creating Party object). (Restriction on item updation after Object Creation)
- 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 :-
- Make the Party class constructor as private
- 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
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.
As the Builder pattern involves in Object creation It comes under Creational Design Pattern.
Example : StringBuilder, AlertBuilder, NotificationBuilder