To říká Builder Pattern „postavte složitý objekt z jednoduchých objektů pomocí přístupu krok za krokem“
Nejčastěji se používá, když objekt nelze vytvořit v jednom kroku, jako je tomu při de-serializaci složitého objektu.
Výhoda návrhového vzoru Builder
Hlavní výhody Builder Pattern jsou následující:
- Poskytuje jasné oddělení mezi konstrukcí a reprezentací objektu.
- Poskytuje lepší kontrolu nad stavebním procesem.
- Podporuje změnu vnitřní reprezentace objektů.
Příklad vzoru UML pro Builder
Příklad návrhového vzoru Builder
Chcete-li vytvořit jednoduchý příklad návrhového vzoru stavitele, musíte provést 6 následujících kroků.
sada vs mapa
- Vytvořte rozhraní pro balení
- Vytvořte 2 abstraktní třídy CD a Společnost
- Vytvořte 2 implementační třídy společnosti: Sony a Samsung
- Vytvořte třídu CDType
- Vytvořte třídu CDBuilder
- Vytvořte třídu BuilderDemo
1) Vytvořte rozhraní Packing
Soubor: Packing.javapublic interface Packing { public String pack(); public int price(); }
2) Vytvořte 2 abstraktní třídy CD a Společnost
Vytvořte CD abstraktní třídy, které bude implementovat rozhraní Packing.
Soubor: CD.javapublic abstract class CD implements Packing{ public abstract String pack(); }Soubor: Company.java
public abstract class Company extends CD{ public abstract int price(); }
3) Vytvořte 2 implementační třídy společnosti: Sony a Samsung
Soubor: Sony.javapublic class Sony extends Company{ @Override public int price(){ return 20; } @Override public String pack(){ return 'Sony CD'; } }//End of the Sony class.Soubor: Samsung.java
public class Samsung extends Company { @Override public int price(){ return 15; } @Override public String pack(){ return 'Samsung CD'; } }//End of the Samsung class.
4) Vytvořte třídu CDType
Soubor: CDType.javaimport java.util.ArrayList; import java.util.List; public class CDType { private List items=new ArrayList(); public void addItem(Packing packs) { items.add(packs); } public void getCost(){ for (Packing packs : items) { packs.price(); } } public void showItems(){ for (Packing packing : items){ System.out.print('CD name : '+packing.pack()); System.out.println(', Price : '+packing.price()); } } }//End of the CDType class.
5) Vytvořte třídu CDBuilder
Soubor: CDBuilder.javapublic class CDBuilder { public CDType buildSonyCD(){ CDType cds=new CDType(); cds.addItem(new Sony()); return cds; } public CDType buildSamsungCD(){ CDType cds=new CDType(); cds.addItem(new Samsung()); return cds; } }// End of the CDBuilder class.
6) Vytvořte třídu BuilderDemo
Soubor: BuilderDemo.javapublic class BuilderDemo{ public static void main(String args[]){ CDBuilder cdBuilder=new CDBuilder(); CDType cdType1=cdBuilder.buildSonyCD(); cdType1.showItems(); CDType cdType2=cdBuilder.buildSamsungCD(); cdType2.showItems(); } }
stáhněte si tento příklad vzoru stavitele
Výstup z výše uvedeného příkladu
CD name : Sony CD, Price : 20 CD name : Samsung CD, Price : 15
Další příklad reálného světa Builder Pattern
Vzor UML pro Builder:
Zvažujeme obchodní případ Pizza Hut kde můžeme získat různé druhy pizzy a studených nápojů.
Pizza může být buď Veg pizza nebo Non-Veg pizza několika druhů (jako sýrová pizza, cibulová pizza, masala-pizza atd.) a bude mít 4 velikosti, tj. malá, střední, velká, extra velká.
vůl vs býk
Studený nápoj může být několika typů (jako Pepsi, Coca-Cola, Dew, Sprite, Fanta, Maaza, Limca, Thums-up atd.) a bude ve 3 velikostech malá, střední, velká.
Příklad vzoru stavitele ze skutečného světa
Podívejme se krok za krokem na reálný příklad Builder Design Pattern.
Krok 1: Vytvořte položku rozhraní, která představuje pizzu a studený nápoj.
Soubor: Item.javapublic interface Item { public String name(); public String size(); public float price(); }// End of the interface Item.
Krok 2: Vytvořte abstraktní třídu Pizza, která bude implementována do rozhraní Item.
dělení řetězců c++Soubor: Pizza.java
public abstract class Pizza implements Item{ @Override public abstract float price(); }
Krok 3: Vytvořte abstraktní třídu ColdDrink, která bude implementována do rozhraní Item.
Soubor: ColdDrink.javapublic abstract class ColdDrink implements Item{ @Override public abstract float price(); }
Krok 4: Vytvořte abstraktní třídu VegPizza, která se rozšíří na abstraktní třídu Pizza.
Soubor: VegPizza.javapublic 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.
Krok 5: Vytvořte abstraktní třídu NonVegPizza, která se rozšíří na abstraktní třídu Pizza.
Soubor: NonVegPizza.javapublic 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.
Krok 6: Nyní vytvořte konkrétní podtřídy SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza, které se rozšíří na abstraktní třídu VegPizza.
Soubor: SmallCheezePizza.javapublic 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.Soubor: 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: LargeCheezePizza.java <pre> 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. </pre> File: ExtraLargeCheezePizza.java <pre> 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. </pre> <p>Step 7: <b>Now, similarly create concrete sub-classes SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza that will extend to the abstract class VegPizza.</b> </p> File: SmallOnionPizza.java <pre> public class SmallOnionPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Small Size'; } }// End of the SmallOnionPizza class. </pre> File: MediumOnionPizza.java <pre> public class MediumOnionPizza extends VegPizza { @Override public float price() { return 150.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Medium Size'; } }// End of the MediumOnionPizza class. </pre> File: LargeOnionPizza.java <pre> public class LargeOnionPizza extends VegPizza{ @Override public float price() { return 180.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Large size'; } }// End of the LargeOnionPizza class. </pre> File: ExtraLargeOnionPizza.java <pre> public class ExtraLargeOnionPizza extends VegPizza { @Override public float price() { return 200.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeOnionPizza class </pre> <p>Step 8: <b>Now, similarly create concrete sub-classes SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza that will extend to the abstract class VegPizza.</b> </p> File: SmallMasalaPizza.java <pre> public class SmallMasalaPizza extends VegPizza{ @Override public float price() { return 100.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Samll Size'; } }// End of the SmallMasalaPizza class </pre> File: MediumMasalaPizza.java <pre> public class MediumMasalaPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Medium Size'; } </pre> File: LargeMasalaPizza.java <pre> public class LargeMasalaPizza extends VegPizza{ @Override public float price() { return 150.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Large Size'; } } //End of the LargeMasalaPizza class </pre> File: ExtraLargeMasalaPizza.java <pre> public class ExtraLargeMasalaPizza extends VegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeMasalaPizza class </pre> <p>Step 9: <b>Now, create concrete sub-classes SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza that will extend to the abstract class NonVegPizza.</b> </p> File: SmallNonVegPizza.java <pre> 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 </pre> File: MediumNonVegPizza.java <pre> public class MediumNonVegPizza extends NonVegPizza{ @Override public float price() { return 200.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Medium Size'; } </pre> File: LargeNonVegPizza.java <pre> public class LargeNonVegPizza extends NonVegPizza{ @Override public float price() { return 220.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Large Size'; } }// End of the LargeNonVegPizza class </pre> File: ExtraLargeNonVegPizza.java <pre> public class ExtraLargeNonVegPizza extends NonVegPizza { @Override public float price() { return 250.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Extra-Large Size'; } } // End of the ExtraLargeNonVegPizza class </pre> <p>Step 10: <b>Now, create two abstract classes Pepsi and Coke that will extend abstract class ColdDrink. </b> </p> File: Pepsi.java <pre> 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 </pre> File: Coke.java <pre> 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 <p>Step 11: <b>Now, create concrete sub-classes SmallPepsi, MediumPepsi, LargePepsi that will extend to the abstract class Pepsi.</b> </p> File: SmallPepsi.java <pre> public class SmallPepsi extends Pepsi{ @Override public String name() { return '300 ml Pepsi'; } @Override public float price() { return 25.0f; } @Override public String size() { return 'Small Size'; } }// End of the SmallPepsi class </pre> File: MediumPepsi.java <pre> public class MediumPepsi extends Pepsi { @Override public String name() { return '500 ml Pepsi'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi class </pre> File: LargePepsi.java <pre> public class LargePepsi extends Pepsi{ @Override public String name() { return '750 ml Pepsi'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class </pre> <p>Step 12: <b>Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke.</b> </p> File: SmallCoke.java <pre> public class SmallCoke extends Coke{ @Override public String name() { return '300 ml Coke'; } @Override public String size() { return 'Small Size'; } @Override public float price() { return 25.0f; } }// End of the SmallCoke class </pre> File: MediumCoke.java <pre> public class MediumCoke extends Coke{ @Override public String name() { return '500 ml Coke'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumCoke class </pre> File: LargeCoke.java <pre> public class LargeCoke extends Coke { @Override public String name() { return '750 ml Coke'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ 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 </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); 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) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); 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) { 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) { 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; 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) { 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; 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()); 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; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); 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('------------------------------------'); 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; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//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()); switch (coldDrink) { 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.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; 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('------------------------'); 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; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4> </pre></pre>Soubor: ExtraLargeCheezePizza.java
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.
Krok 7: Nyní podobným způsobem vytvořte konkrétní podtřídy SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza, které se rozšíří na abstraktní třídu VegPizza.
Soubor: SmallOnionPizza.javapublic class SmallOnionPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Small Size'; } }// End of the SmallOnionPizza class.Soubor: MediumOnionPizza.java
public class MediumOnionPizza extends VegPizza { @Override public float price() { return 150.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Medium Size'; } }// End of the MediumOnionPizza class.Soubor: LargeOnionPizza.java
public class LargeOnionPizza extends VegPizza{ @Override public float price() { return 180.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Large size'; } }// End of the LargeOnionPizza class.Soubor: ExtraLargeOnionPizza.java
public class ExtraLargeOnionPizza extends VegPizza { @Override public float price() { return 200.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeOnionPizza class
Krok 8: Nyní podobným způsobem vytvořte konkrétní podtřídy SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza, které se rozšíří na abstraktní třídu VegPizza.
Soubor: SmallMasalaPizza.javapublic class SmallMasalaPizza extends VegPizza{ @Override public float price() { return 100.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Samll Size'; } }// End of the SmallMasalaPizza classSoubor: MediumMasalaPizza.java
public class MediumMasalaPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Medium Size'; }Soubor: LargeMasalaPizza.java
public class LargeMasalaPizza extends VegPizza{ @Override public float price() { return 150.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Large Size'; } } //End of the LargeMasalaPizza classSoubor: ExtraLargeMasalaPizza.java
public class ExtraLargeMasalaPizza extends VegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeMasalaPizza class
Krok 9: Nyní vytvořte konkrétní podtřídy SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza, které se rozšíří na abstraktní třídu NonVegPizza.
Soubor: SmallNonVegPizza.javapublic 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 classSoubor: MediumNonVegPizza.java
public class MediumNonVegPizza extends NonVegPizza{ @Override public float price() { return 200.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Medium Size'; }Soubor: LargeNonVegPizza.java
public class LargeNonVegPizza extends NonVegPizza{ @Override public float price() { return 220.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Large Size'; } }// End of the LargeNonVegPizza classSoubor: ExtraLargeNonVegPizza.java
public class ExtraLargeNonVegPizza extends NonVegPizza { @Override public float price() { return 250.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Extra-Large Size'; } } // End of the ExtraLargeNonVegPizza class
Krok 10: Nyní vytvořte dvě abstraktní třídy Pepsi a Cola, které rozšíří abstraktní třídu ColdDrink.
Soubor: Pepsi.javapublic abstract class Pepsi extends ColdDrink { @Override public abstract String name(); @Override public abstract String size(); @Override public abstract float price(); }// End of the Pepsi classSoubor: Coke.java
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 <p>Step 11: <b>Now, create concrete sub-classes SmallPepsi, MediumPepsi, LargePepsi that will extend to the abstract class Pepsi.</b> </p> File: SmallPepsi.java <pre> public class SmallPepsi extends Pepsi{ @Override public String name() { return '300 ml Pepsi'; } @Override public float price() { return 25.0f; } @Override public String size() { return 'Small Size'; } }// End of the SmallPepsi class </pre> File: MediumPepsi.java <pre> public class MediumPepsi extends Pepsi { @Override public String name() { return '500 ml Pepsi'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi class </pre> File: LargePepsi.java <pre> public class LargePepsi extends Pepsi{ @Override public String name() { return '750 ml Pepsi'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class </pre> <p>Step 12: <b>Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke.</b> </p> File: SmallCoke.java <pre> public class SmallCoke extends Coke{ @Override public String name() { return '300 ml Coke'; } @Override public String size() { return 'Small Size'; } @Override public float price() { return 25.0f; } }// End of the SmallCoke class </pre> File: MediumCoke.java <pre> public class MediumCoke extends Coke{ @Override public String name() { return '500 ml Coke'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumCoke class </pre> File: LargeCoke.java <pre> public class LargeCoke extends Coke { @Override public String name() { return '750 ml Coke'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ 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 </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); 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) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); 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) { 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) { 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; 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) { 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; 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()); 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; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); 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('------------------------------------'); 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; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//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()); switch (coldDrink) { 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.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; 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('------------------------'); 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; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4> </pre>Soubor: MediumPepsi.java
public class MediumPepsi extends Pepsi { @Override public String name() { return '500 ml Pepsi'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi classSoubor: LargePepsi.java
public class LargePepsi extends Pepsi{ @Override public String name() { return '750 ml Pepsi'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class
Krok 12: Nyní vytvořte konkrétní podtřídy SmallCoke, MediumCoke, LargeCoke, které se rozšíří na abstraktní třídu Coke.
objektivní javaSoubor: SmallCoke.java
public class SmallCoke extends Coke{ @Override public String name() { return '300 ml Coke'; } @Override public String size() { return 'Small Size'; } @Override public float price() { return 25.0f; } }// End of the SmallCoke classSoubor: MediumCoke.java
public class MediumCoke extends Coke{ @Override public String name() { return '500 ml Coke'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumCoke classSoubor: LargeCoke.java
public class LargeCoke extends Coke { @Override public String name() { return '750 ml Coke'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ 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 </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); 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) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); 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) { 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) { 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; 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) { 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; 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()); 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; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); 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('------------------------------------'); 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; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//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()); switch (coldDrink) { 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.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; 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('------------------------'); 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; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>
Krok 14: Vytvořte třídu OrderBuilder, která bude zodpovědná za vytváření objektů třídy OrderedItems.
Soubor: OrdereBuilder.javaimport java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); 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) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); 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) { 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) { 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; 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) { 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; 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()); 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; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); 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('------------------------------------'); 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; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//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()); switch (coldDrink) { 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.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; 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('------------------------'); 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; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method
Krok 15: Vytvořte třídu BuilderDemo, která bude používat třídu OrderBuilder.
Soubor: Prototype.javaimport java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class
stáhněte si tento příklad vzoru Builder