Javatpoint Logo

91-9990449935

 0120-4256464

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:

  • It provides clear separation between the construction and representation of an object.
  • It provides better control over construction process.
  • It supports to change the internal representation of objects.

UML for Builder Pattern Example

Builder Design Pattern

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
  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

1) Create Packing interface

File: Packing.java

2) Create 2 abstract classes CD and Company

Create an abstract class CD which will implement Packing interface.

File: CD.java
File: Company.java

3) Create 2 implementation classes of Company: Sony and Samsung

File: Sony.java
File: Samsung.java

4) Create the CDType class

File: CDType.java

5) Create the CDBuilder class

File: CDBuilder.java

6) Create the BuilderDemo class

File: BuilderDemo.java


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.

Builder Design Pattern uml

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.

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

Output

builderDesign Pattern output 1 builder Design Pattern output 1