Builder Design Pattern
Builder Pattern says that "construct a complex object from simple objects using step-by-step approach"
It is mostly used when object can't be created in single step like in the de-serialization of a complex object.
Advantage of Builder Design Pattern
The main advantages of Builder Pattern are as follows:
UML for Builder Pattern Example
Example of Builder Design Pattern
To create simple example of builder design pattern, you need to follow 6 following steps.
1) Create Packing interface
2) Create 2 abstract classes CD and Company
Create an abstract class CD which will implement Packing interface.
3) Create 2 implementation classes of Company: Sony and Samsung
4) Create the CDType class
5) Create the CDBuilder class
6) Create the BuilderDemo class
Output of the above example
Another Real world example of Builder Pattern
UML for Builder Pattern:
We are considering a business case of pizza-hut where we can get different varieties of pizza and cold-drink.
Pizza can be either a Veg pizza or Non-Veg pizza of several types (like cheese pizza, onion pizza, masala-pizza etc) and will be of 4 sizes i.e. small, medium, large, extra-large.
Cold-drink can be of several types (like Pepsi, Coke, Dew, Sprite, Fanta, Maaza, Limca, Thums-up etc.) and will be of 3 sizes small, medium, large.
Real world example of builder pattern
Let's see the step by step real world example of Builder Design Pattern.
Step 1:Create an interface Item that represents the Pizza and Cold-drink.
Step 2:Create an abstract class Pizza that will implement to the interface Item.
Step 3:Create an abstract class ColdDrink that will implement to the interface Item.
Step 4:Create an abstract class VegPizza that will extend to the abstract class Pizza.
Step 5:Create an abstract class NonVegPizza that will extend to the abstract class Pizza.
Step 6:Now, create concrete sub-classes SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza that will extend to the abstract class VegPizza.
Step 7:Now, similarly create concrete sub-classes SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza that will extend to the abstract class VegPizza.
Step 8:Now, similarly create concrete sub-classes SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza that will extend to the abstract class VegPizza.
Step 9:Now, create concrete sub-classes SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza that will extend to the abstract class NonVegPizza.
Step 10:Now, create two abstract classes Pepsi and Coke that will extend abstract class ColdDrink.
Step 11:Now, create concrete sub-classes SmallPepsi, MediumPepsi, LargePepsi that will extend to the abstract class Pepsi.
Step 12:Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke.
Step 13:Create an OrderedItems class that are having Item objects defined above.
Step 14:Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.
Step 15:Create a BuilderDemo class that will use the OrderBuilder class.