Builder Design PatternBuilder 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 PatternThe main advantages of Builder Pattern are as follows:
UML for Builder Pattern ExampleExample of Builder Design PatternTo create simple example of builder design pattern, you need to follow 6 following steps.
1) Create Packing interfaceFile: Packing.java 2) Create 2 abstract classes CD and CompanyCreate an abstract class CD which will implement Packing interface. File: CD.java File: Company.java 3) Create 2 implementation classes of Company: Sony and SamsungFile: Sony.java File: Samsung.java 4) Create the CDType classFile: CDType.java 5) Create the CDBuilder classFile: CDBuilder.java 6) Create the BuilderDemo classFile: BuilderDemo.java Output of the above exampleAnother Real world example of Builder PatternUML 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 patternLet'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. File: Item.java Step 2:Create an abstract class Pizza that will implement to the interface Item. File: Pizza.java Step 3:Create an abstract class ColdDrink that will implement to the interface Item. File: ColdDrink.java Step 4:Create an abstract class VegPizza that will extend to the abstract class Pizza. File: VegPizza.java Step 5:Create an abstract class NonVegPizza that will extend to the abstract class Pizza. File: NonVegPizza.java Step 6:Now, create concrete sub-classes SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza that will extend to the abstract class VegPizza. File: SmallCheezePizza.java File: MediumCheezePizza.java File: ExtraLargeCheezePizza.java Step 7:Now, similarly create concrete sub-classes SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza that will extend to the abstract class VegPizza. File: SmallOnionPizza.java File: MediumOnionPizza.java File: LargeOnionPizza.java File: ExtraLargeOnionPizza.java Step 8:Now, similarly create concrete sub-classes SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza that will extend to the abstract class VegPizza. File: SmallMasalaPizza.java File: MediumMasalaPizza.java File: LargeMasalaPizza.java File: ExtraLargeMasalaPizza.java Step 9:Now, create concrete sub-classes SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza that will extend to the abstract class NonVegPizza. File: SmallNonVegPizza.java File: MediumNonVegPizza.java File: LargeNonVegPizza.java File: ExtraLargeNonVegPizza.java Step 10:Now, create two abstract classes Pepsi and Coke that will extend abstract class ColdDrink. File: Pepsi.java File: Coke.java File: MediumPepsi.java File: LargePepsi.java Step 12:Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke. File: SmallCoke.java File: MediumCoke.java File: LargeCoke.java Step 14:Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class. File: OrdereBuilder.java Step 15:Create a BuilderDemo class that will use the OrderBuilder class. File: Prototype.java OutputNext TopicObject Pool Pattern |