The builder pattern allows for step-by-step creation of complex objects using simple objects. It provides separation of construction and representation and supports changing the internal representation. The builder pattern is used in a pizza ordering system where a director uses a builder to construct an order with various pizza and drink items. Concrete builders create specific pizza and drink objects that are added to an ordered items object.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
24 views
09 - Builder Pattern - Week7
The builder pattern allows for step-by-step creation of complex objects using simple objects. It provides separation of construction and representation and supports changing the internal representation. The builder pattern is used in a pizza ordering system where a director uses a builder to construct an order with various pizza and drink items. Concrete builders create specific pizza and drink objects that are added to an ordered items object.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43
Builder Pattern
Builder Pattern • The builder pattern is a design pattern that allows for: The step-by-step creation of complex objects Using the correct sequence of actions.
• Builder pattern builds a complex object using simple
objects and using a step by step approach. Advantages • 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. Elements of Builder Pattern • Director • Builder • Concrete Builder • Product
• Director asks builder to build the order(concrete builder)
using product Pizza Hut Case study • 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. Step 1: • Create an interface Item that represents the Pizza and Cold- drink. File: Item.java public interface Item { public String name(); public String size(); public float price(); }// End of the interface Item. Step 2: • Create an abstract class Pizza that will implement to the interface Item. File: Pizza.java public abstract class Pizza implements Item{ @Override public abstract float price(); } Step 3: • Create an abstract class ColdDrink that will implement to the interface Item. File: ColdDrink.java public abstract class ColdDrink implements Item{ @Override public abstract float price(); Step 4: • Create an abstract class VegPizza that will extend to the abstract class Pizza. File: VegPizza.java public abstract class VegPizza extends Pizza{ @Override public abstract float price(); @Override public abstract String name(); @Override public abstract String size(); }// End of the abstract class VegPizza. Step 5: • Create an abstract class NonVegPizza that will extend to the abstract class Pizza. File: NonVegPizza.java public abstract class NonVegPizza extends Pizza{ @Override public abstract float price(); @Override public abstract String name(); @Override public abstract String size(); }// End of the abstract class NonVegPizza. Step 6: • Now, create concrete sub-classes SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza that will extend to the abstract class VegPizza. File: SmallCheezePizza.java public class SmallCheezePizza extends VegPizza{ @Override public float price() { return 170.0f; } @Override public String name() { return "Cheeze Pizza"; } @Override public String size() { return "Small size"; } }// End of the SmallCheezePizza class. File: MediumCheezePizza.java public class MediumCheezePizza extends VegPizza{ @Override public float price() { return 220.f; } @Override public String name() { return "Cheeze Pizza"; } @Override public String size() { return "Medium Size"; } }// End of the MediumCheezePizza class. File: Large CheezePizza.java public class LargeCheezePizza extends VegPizza{ @Override public float price() { return 260.0f; } @Override public String name() { return "Cheeze Pizza"; } @Override public String size() { return "Large Size"; } }// End of the LargeCheezePizza class. File: ExtraLargeCheezePizza public class ExtraLargeCheezePizza extends VegPizza{ @Override public float price() { return 300.f; } @Override public String name() { return "Cheeze Pizza"; } @Override public String size() { return "Extra-Large Size"; } }// End of the ExtraLargeCheezePizza class. Other Classes • Step 7:Now, similarly create concrete sub-classes SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza that will extend to the abstract class VegPizza.
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. File: SmallNonVegPizza.java public class SmallNonVegPizza extends NonVegPizza {
@Override public float price() { return 180.0f; }
@Override public String name() { return "Non-Veg Pizza"; }
@Override public String size() { return "Samll Size"; }
}// End of the SmallNonVegPizza class Step 10: • Now, create two abstract classes Pepsi and Coke that will extend abstract class ColdDrink. public abstract class Pepsi extends ColdDrink {
@Override public abstract String name();
@Override public abstract String size();
@Override public abstract float price();
}// End of the Pepsi class Step 10: public abstract class Coke extends ColdDrink {
@Override public abstract String name();
@Override public abstract String size();
@Override public abstract float price();
}// End of the Coke class Other Classes • Step 11: Now, create concrete sub classes SmallPepsi, MediumPepsi, LargePepsi that will extend t o 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. import java.util.ArrayList; import java.util.List; public class OrderedItems {
for (Item item : items) { System.out.println("Item is:" +item.name()); System.out.println("Size is:" +item.size()); System.out.println("Price is: " +item.price());
} }
}// End of the OrderedItems class Step 14: • Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{
Step 14: System.out.println(" Enter the choice of Pizza "); System.out.println("============================"); System.out.println(" 1. Veg-Pizza "); System.out.println(" 2. Non-Veg Pizza "); System.out.println(" 3. Exit "); System.out.println("============================");
int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine());
switch(pizzaandcolddrinkchoice) Step 14: { case 1:{
System.out.println("You ordered Veg Pizza"); System.out.println("\n\n"); System.out.println(" Enter the types of Veg-Pizza "); System.out.println("------------------------------"); System.out.println(" 1.Cheeze Pizza "); System.out.println(" 2.Onion Pizza "); System.out.println(" 3.Masala Pizza "); System.out.println(" 4.Exit "); System.out.println("------------------------------"); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) Step 14: { case 1: { System.out.println("You ordered Cheeze Pizza");
System.out.println("Enter the cheeze pizza size"); System.out.println("------------------------------------"); System.out.println(" 1. Small Cheeze Pizza "); System.out.println(" 2. Medium Cheeze Pizza "); System.out.println(" 3. Large Cheeze Pizza "); System.out.println(" 4. Extra-Large Cheeze Pizza "); System.out.println("------------------------------------"); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) Step 14: { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; Step 14: case 2: { System.out.println("You ordered Onion Pizza"); System.out.println("Enter the Onion pizza size"); System.out.println("----------------------------------"); System.out.println(" 1. Small Onion Pizza "); System.out.println(" 2. Medium Onion Pizza "); System.out.println(" 3. Large Onion Pizza "); System.out.println(" 4. Extra- Large Onion Pizza "); System.out.println("----------------------------------"); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) Step 14: • { case 1: itemsOrder.addItems(new SmallOnionPizza()); break;
case 2: itemsOrder.addItems(new MediumOnionPizza()); break;
case 3: itemsOrder.addItems(new LargeOnionPizza()); break;
case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break;
} } break; Step 14: case 3: { System.out.println("You ordered Masala Pizza"); System.out.println("Enter the Masala pizza size"); System.out.println("------------------------------------"); System.out.println(" 1. Small Masala Pizza "); System.out.println(" 2. Medium Masala Pizza "); System.out.println(" 3. Large Masala Pizza "); System.out.println(" 4. Extra-Large Masala Pizza "); System.out.println("------------------------------------"); int masalapizzasize=Integer.parseInt(br.readLine()); Step 14: switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break;
case 2: itemsOrder.addItems(new MediumMasalaPizza()); break;
case 3: itemsOrder.addItems(new LargeMasalaPizza()); break;
case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break;
System.out.println("Enter the Non-Veg pizza size"); System.out.println("------------------------------------"); System.out.println(" 1. Small Non-Veg Pizza "); System.out.println(" 2. Medium Non-Veg Pizza "); System.out.println(" 3. Large Non-Veg Pizza "); System.out.println(" 4. Extra-Large Non- Veg Pizza "); System.out.println("------------------------------------"); Step 14: int nonvegpizzasize=Integer.parseInt(br.readLine());
switch(nonvegpizzasize) {
case 1: itemsOrder.addItems(new SmallNonVegPizza()); break;
case 2: itemsOrder.addItems(new MediumNonVegPizza()); break;
case 3: itemsOrder.addItems(new LargeNonVegPizza()); break;
Step 14: case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; }
} break; default: { break;
} Step 14 }//end of main Switch
//continued?.. System.out.println(" Enter the choice of ColdDrink "); System.out.println("============================"); System.out.println(" 1. Pepsi "); System.out.println(" 2. Coke "); System.out.println(" 3. Exit "); System.out.println("============================"); int coldDrink=Integer.parseInt(br.readLine()); Step 14: { case 1: { System.out.println("You ordered Pepsi "); System.out.println("Enter the Pepsi Size "); System.out.println("------------------------"); System.out.println(" 1. Small Pepsi "); System.out.println(" 2. Medium Pepsi "); System.out.println(" 3. Large Pepsi "); System.out.println("------------------------"); int pepsisize=Integer.parseInt(br.readLi ne()); Step 14: switch (coldDrink) switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break;
case 2: itemsOrder.addItems(new MediumPepsi()); break;
case 3: itemsOrder.addItems(new LargePepsi()); break;
} } break;
Step 14: case 2: { System.out.println("You ordered Coke"); System.out.println("Enter the Coke Size"); System.out.println("------------------------"); System.out.println(" 1. Small Coke "); System.out.println(" 2. Medium Coke "); System.out.println(" 3. Large Coke "); System.out.println(" 4. Extra-Large Coke "); System.out.println("------------------------");
Step 14: int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break;
case 2: itemsOrder.addItems(new MediumCoke()); break;
case 3: itemsOrder.addItems(new LargeCoke()); break;
}
}
Step 14: break; default: { break;
}
}//End of the Cold-Drink switch return itemsOrder;
} //End of the preparePizza() method Step 15: • Create a BuilderDemo class that will use the OrderBuilder class. import java.io.IOException; public class BuilderDemo {
public static void main(String[] args) throws IOException { // TODO code application logic here