logo

Builder Design Pattern

  1. Oblikovalski vzorec Builder
  2. Prednost Builder DP
  3. Uporaba Builder DP
  4. UML Builder DP
  5. Primer Builder DP

Builder Pattern pravi to 'sestavi kompleksen objekt iz preprostih predmetov s pristopom korak za korakom'

Večinoma se uporablja, ko objekta ni mogoče ustvariti v enem samem koraku, kot pri deserializaciji kompleksnega objekta.

Prednost Builder Design Pattern

Glavne prednosti Builder Pattern so naslednje:

  • Omogoča jasno ločevanje med konstrukcijo in predstavitvijo predmeta.
  • Omogoča boljši nadzor nad procesom gradnje.
  • Podpira spreminjanje notranje predstavitve predmetov.

UML za primer vzorca Builder


Primer oblikovalskega vzorca Builder

Če želite ustvariti preprost primer oblikovalskega vzorca graditelja, morate slediti 6 naslednjim korakom.

  1. Ustvari vmesnik za pakiranje
  2. Ustvarite 2 abstraktna razreda CD in Company
  3. Ustvarite 2 izvedbena razreda podjetja: Sony in Samsung
  4. Ustvarite razred CDType
  5. Ustvarite razred CDBuilder
  6. Ustvarite razred BuilderDemo

1) Ustvarite vmesnik za pakiranje

Datoteka: Packing.java
 public interface Packing { public String pack(); public int price(); } 

2) Ustvarite 2 abstraktna razreda CD in Company

Ustvarite CD z abstraktnim razredom, ki bo izvajal vmesnik za pakiranje.

Datoteka: CD.java
 public abstract class CD implements Packing{ public abstract String pack(); } 
Datoteka: Company.java
 public abstract class Company extends CD{ public abstract int price(); } 

3) Ustvarite 2 izvedbena razreda podjetja: Sony in Samsung

Datoteka: Sony.java
 public class Sony extends Company{ @Override public int price(){ return 20; } @Override public String pack(){ return 'Sony CD'; } }//End of the Sony class. 
Datoteka: 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) Ustvarite razred CDType

Datoteka: CDType.java
 import 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) Ustvarite razred CDBuilder

Datoteka: CDBuilder.java
 public 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) Ustvarite razred BuilderDemo

Datoteka: BuilderDemo.java
 public class BuilderDemo{ public static void main(String args[]){ CDBuilder cdBuilder=new CDBuilder(); CDType cdType1=cdBuilder.buildSonyCD(); cdType1.showItems(); CDType cdType2=cdBuilder.buildSamsungCD(); cdType2.showItems(); } } 

prenesite ta primer vzorca graditelja

Rezultat zgornjega primera

 CD name : Sony CD, Price : 20 CD name : Samsung CD, Price : 15 

Še en primer Builder Pattern iz resničnega sveta

UML za vzorec graditelja:

Razmišljamo o poslovnem primeru pizza-hut kjer lahko dobimo različne vrste pic in hladnih pijač.

Pizza je lahko zelenjavna ali nezelenjavna pica več vrst (na primer pica s sirom, čebulna pica, masala pica itd.) in bo v 4 velikostih, tj. majhna, srednja, velika, zelo velika.

linux zaženi cmd

Hladna pijača je lahko več vrst (npr. Pepsi, Coca-Cola, Dew, Sprite, Fanta, Maaza, Limca, Thums-up itd.) in bo v treh velikostih: mala, srednja, velika.

Primer vzorca graditelja iz resničnega sveta

Oglejmo si korak za korakom primer vzorca načrtovanja Builder v resničnem svetu.

Korak 1: Ustvarite element vmesnika, ki predstavlja pico in hladno pijačo.

Datoteka: Item.java
 public interface Item { public String name(); public String size(); public float price(); }// End of the interface Item. 

2. korak: Ustvarite abstraktni razred Pizza, ki bo implementiran v vmesnik Item.

Datoteka: Pizza.java
 public abstract class Pizza implements Item{ @Override public abstract float price(); } 

3. korak: Ustvarite abstraktni razred ColdDrink, ki bo implementiran v vmesnik Item.

Datoteka: ColdDrink.java
 public abstract class ColdDrink implements Item{ @Override public abstract float price(); } 

4. korak: Ustvarite abstraktni razred VegPizza, ki se bo razširil na abstraktni razred Pizza.

Datoteka: 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. 

5. korak: Ustvarite abstrakten razred NonVegPizza, ki se bo razširil na abstraktni razred Pizza.

Datoteka: 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. 

6. korak: Zdaj ustvarite konkretne podrazrede SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza, ki se bodo razširili na abstraktni razred VegPizza.

Datoteka: 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. 
Datoteka: MediumCheezePizza.java
 public class MediumCheezePizza extends VegPizza{ @Override public float price() { return 220.f; } @Override public String name() { return &apos;Cheeze Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } }// 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 &apos;Cheeze Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } }// 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 &apos;Cheeze Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// 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 &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Small Size&apos;; } }// 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 &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } }// 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 &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Large size&apos;; } }// 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 &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// 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 &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Samll Size&apos;; } }// 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 &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } </pre> File: LargeMasalaPizza.java <pre> public class LargeMasalaPizza extends VegPizza{ @Override public float price() { return 150.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } } //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 &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// 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 &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Samll Size&apos;; } }// 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 &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } </pre> File: LargeNonVegPizza.java <pre> public class LargeNonVegPizza extends NonVegPizza{ @Override public float price() { return 220.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } }// 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 &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } } // 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 &apos;300 ml Pepsi&apos;; } @Override public float price() { return 25.0f; } @Override public String size() { return &apos;Small Size&apos;; } }// End of the SmallPepsi class </pre> File: MediumPepsi.java <pre> public class MediumPepsi extends Pepsi { @Override public String name() { return &apos;500 ml Pepsi&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @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 &apos;750 ml Pepsi&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @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 &apos;300 ml Coke&apos;; } @Override public String size() { return &apos;Small Size&apos;; } @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 &apos;500 ml Coke&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @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 &apos;750 ml Coke&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @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(&apos;Item is:&apos; +item.name()); System.out.println(&apos;Size is:&apos; +item.size()); System.out.println(&apos;Price is: &apos; +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(&apos; Enter the choice of Pizza &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Veg-Pizza &apos;); System.out.println(&apos; 2. Non-Veg Pizza &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println(&apos;You ordered Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos; Enter the types of Veg-Pizza &apos;); System.out.println(&apos;------------------------------&apos;); System.out.println(&apos; 1.Cheeze Pizza &apos;); System.out.println(&apos; 2.Onion Pizza &apos;); System.out.println(&apos; 3.Masala Pizza &apos;); System.out.println(&apos; 4.Exit &apos;); System.out.println(&apos;------------------------------&apos;); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println(&apos;You ordered Cheeze Pizza&apos;); System.out.println(&apos;Enter the cheeze pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Cheeze Pizza &apos;); System.out.println(&apos; 2. Medium Cheeze Pizza &apos;); System.out.println(&apos; 3. Large Cheeze Pizza &apos;); System.out.println(&apos; 4. Extra-Large Cheeze Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos;You ordered Onion Pizza&apos;); System.out.println(&apos;Enter the Onion pizza size&apos;); System.out.println(&apos;----------------------------------&apos;); System.out.println(&apos; 1. Small Onion Pizza &apos;); System.out.println(&apos; 2. Medium Onion Pizza &apos;); System.out.println(&apos; 3. Large Onion Pizza &apos;); System.out.println(&apos; 4. Extra-Large Onion Pizza &apos;); System.out.println(&apos;----------------------------------&apos;); 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(&apos;You ordered Masala Pizza&apos;); System.out.println(&apos;Enter the Masala pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Masala Pizza &apos;); System.out.println(&apos; 2. Medium Masala Pizza &apos;); System.out.println(&apos; 3. Large Masala Pizza &apos;); System.out.println(&apos; 4. Extra-Large Masala Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos;You ordered Non-Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos;Enter the Non-Veg pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Non-Veg Pizza &apos;); System.out.println(&apos; 2. Medium Non-Veg Pizza &apos;); System.out.println(&apos; 3. Large Non-Veg Pizza &apos;); System.out.println(&apos; 4. Extra-Large Non-Veg Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos; Enter the choice of ColdDrink &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Pepsi &apos;); System.out.println(&apos; 2. Coke &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println(&apos;You ordered Pepsi &apos;); System.out.println(&apos;Enter the Pepsi Size &apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Pepsi &apos;); System.out.println(&apos; 2. Medium Pepsi &apos;); System.out.println(&apos; 3. Large Pepsi &apos;); System.out.println(&apos;------------------------&apos;); 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(&apos;You ordered Coke&apos;); System.out.println(&apos;Enter the Coke Size&apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Coke &apos;); System.out.println(&apos; 2. Medium Coke &apos;); System.out.println(&apos; 3. Large Coke &apos;); System.out.println(&apos; 4. Extra-Large Coke &apos;); System.out.println(&apos;------------------------&apos;); 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(&apos;
&apos;); System.out.println(&apos;Total Cost : &apos;+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>  </pre></pre>
Datoteka: ExtraLargeCheezePizza.java
 public class ExtraLargeCheezePizza extends VegPizza{ @Override public float price() { return 300.f; } @Override public String name() { return &apos;Cheeze Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeCheezePizza class. 

7. korak: Zdaj na podoben način ustvarite konkretne podrazrede SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza, ki se bodo razširili na abstraktni razred VegPizza.

Datoteka: SmallOnionPizza.java
 public class SmallOnionPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Small Size&apos;; } }// End of the SmallOnionPizza class. 
Datoteka: MediumOnionPizza.java
 public class MediumOnionPizza extends VegPizza { @Override public float price() { return 150.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } }// End of the MediumOnionPizza class. 
Datoteka: LargeOnionPizza.java
 public class LargeOnionPizza extends VegPizza{ @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Large size&apos;; } }// End of the LargeOnionPizza class. 
Datoteka: ExtraLargeOnionPizza.java
 public class ExtraLargeOnionPizza extends VegPizza { @Override public float price() { return 200.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeOnionPizza class 

8. korak: Zdaj na podoben način ustvarite konkretne podrazrede SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza, ki se bodo razširili na abstraktni razred VegPizza.

Datoteka: SmallMasalaPizza.java
 public class SmallMasalaPizza extends VegPizza{ @Override public float price() { return 100.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Samll Size&apos;; } }// End of the SmallMasalaPizza class 
Datoteka: MediumMasalaPizza.java
 public class MediumMasalaPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } 
Datoteka: LargeMasalaPizza.java
 public class LargeMasalaPizza extends VegPizza{ @Override public float price() { return 150.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } } //End of the LargeMasalaPizza class 
Datoteka: ExtraLargeMasalaPizza.java
 public class ExtraLargeMasalaPizza extends VegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeMasalaPizza class 

9. korak: Zdaj ustvarite konkretne podrazrede SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza, ki se bodo razširili na abstraktni razred NonVegPizza.

Datoteka: SmallNonVegPizza.java
 public class SmallNonVegPizza extends NonVegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Samll Size&apos;; } }// End of the SmallNonVegPizza class 
Datoteka: MediumNonVegPizza.java
 public class MediumNonVegPizza extends NonVegPizza{ @Override public float price() { return 200.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } 
Datoteka: LargeNonVegPizza.java
 public class LargeNonVegPizza extends NonVegPizza{ @Override public float price() { return 220.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } }// End of the LargeNonVegPizza class 
Datoteka: ExtraLargeNonVegPizza.java
 public class ExtraLargeNonVegPizza extends NonVegPizza { @Override public float price() { return 250.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } } // End of the ExtraLargeNonVegPizza class 

10. korak: Zdaj ustvarite dva abstraktna razreda Pepsi in Coca-Cola, ki bosta razširila abstraktni razred ColdDrink.

Datoteka: Pepsi.java
 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 
Datoteka: 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 &apos;300 ml Pepsi&apos;; } @Override public float price() { return 25.0f; } @Override public String size() { return &apos;Small Size&apos;; } }// End of the SmallPepsi class </pre> File: MediumPepsi.java <pre> public class MediumPepsi extends Pepsi { @Override public String name() { return &apos;500 ml Pepsi&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @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 &apos;750 ml Pepsi&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @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 &apos;300 ml Coke&apos;; } @Override public String size() { return &apos;Small Size&apos;; } @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 &apos;500 ml Coke&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @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 &apos;750 ml Coke&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @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(&apos;Item is:&apos; +item.name()); System.out.println(&apos;Size is:&apos; +item.size()); System.out.println(&apos;Price is: &apos; +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(&apos; Enter the choice of Pizza &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Veg-Pizza &apos;); System.out.println(&apos; 2. Non-Veg Pizza &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println(&apos;You ordered Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos; Enter the types of Veg-Pizza &apos;); System.out.println(&apos;------------------------------&apos;); System.out.println(&apos; 1.Cheeze Pizza &apos;); System.out.println(&apos; 2.Onion Pizza &apos;); System.out.println(&apos; 3.Masala Pizza &apos;); System.out.println(&apos; 4.Exit &apos;); System.out.println(&apos;------------------------------&apos;); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println(&apos;You ordered Cheeze Pizza&apos;); System.out.println(&apos;Enter the cheeze pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Cheeze Pizza &apos;); System.out.println(&apos; 2. Medium Cheeze Pizza &apos;); System.out.println(&apos; 3. Large Cheeze Pizza &apos;); System.out.println(&apos; 4. Extra-Large Cheeze Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos;You ordered Onion Pizza&apos;); System.out.println(&apos;Enter the Onion pizza size&apos;); System.out.println(&apos;----------------------------------&apos;); System.out.println(&apos; 1. Small Onion Pizza &apos;); System.out.println(&apos; 2. Medium Onion Pizza &apos;); System.out.println(&apos; 3. Large Onion Pizza &apos;); System.out.println(&apos; 4. Extra-Large Onion Pizza &apos;); System.out.println(&apos;----------------------------------&apos;); 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(&apos;You ordered Masala Pizza&apos;); System.out.println(&apos;Enter the Masala pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Masala Pizza &apos;); System.out.println(&apos; 2. Medium Masala Pizza &apos;); System.out.println(&apos; 3. Large Masala Pizza &apos;); System.out.println(&apos; 4. Extra-Large Masala Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos;You ordered Non-Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos;Enter the Non-Veg pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Non-Veg Pizza &apos;); System.out.println(&apos; 2. Medium Non-Veg Pizza &apos;); System.out.println(&apos; 3. Large Non-Veg Pizza &apos;); System.out.println(&apos; 4. Extra-Large Non-Veg Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos; Enter the choice of ColdDrink &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Pepsi &apos;); System.out.println(&apos; 2. Coke &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println(&apos;You ordered Pepsi &apos;); System.out.println(&apos;Enter the Pepsi Size &apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Pepsi &apos;); System.out.println(&apos; 2. Medium Pepsi &apos;); System.out.println(&apos; 3. Large Pepsi &apos;); System.out.println(&apos;------------------------&apos;); 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(&apos;You ordered Coke&apos;); System.out.println(&apos;Enter the Coke Size&apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Coke &apos;); System.out.println(&apos; 2. Medium Coke &apos;); System.out.println(&apos; 3. Large Coke &apos;); System.out.println(&apos; 4. Extra-Large Coke &apos;); System.out.println(&apos;------------------------&apos;); 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(&apos;
&apos;); System.out.println(&apos;Total Cost : &apos;+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>  </pre>
Datoteka: MediumPepsi.java
 public class MediumPepsi extends Pepsi { @Override public String name() { return &apos;500 ml Pepsi&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi class 
Datoteka: LargePepsi.java
 public class LargePepsi extends Pepsi{ @Override public String name() { return &apos;750 ml Pepsi&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class 

12. korak: Zdaj ustvarite konkretne podrazrede SmallCoke, MediumCoke, LargeCoke, ki se bodo razširili na abstraktni razred Coke.

Datoteka: SmallCoke.java
 public class SmallCoke extends Coke{ @Override public String name() { return &apos;300 ml Coke&apos;; } @Override public String size() { return &apos;Small Size&apos;; } @Override public float price() { return 25.0f; } }// End of the SmallCoke class 
Datoteka: MediumCoke.java
 public class MediumCoke extends Coke{ @Override public String name() { return &apos;500 ml Coke&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @Override public float price() { return 35.0f; } }// End of the MediumCoke class 
Datoteka: LargeCoke.java
 public class LargeCoke extends Coke { @Override public String name() { return &apos;750 ml Coke&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @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(&apos;Item is:&apos; +item.name()); System.out.println(&apos;Size is:&apos; +item.size()); System.out.println(&apos;Price is: &apos; +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(&apos; Enter the choice of Pizza &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Veg-Pizza &apos;); System.out.println(&apos; 2. Non-Veg Pizza &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println(&apos;You ordered Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos; Enter the types of Veg-Pizza &apos;); System.out.println(&apos;------------------------------&apos;); System.out.println(&apos; 1.Cheeze Pizza &apos;); System.out.println(&apos; 2.Onion Pizza &apos;); System.out.println(&apos; 3.Masala Pizza &apos;); System.out.println(&apos; 4.Exit &apos;); System.out.println(&apos;------------------------------&apos;); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println(&apos;You ordered Cheeze Pizza&apos;); System.out.println(&apos;Enter the cheeze pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Cheeze Pizza &apos;); System.out.println(&apos; 2. Medium Cheeze Pizza &apos;); System.out.println(&apos; 3. Large Cheeze Pizza &apos;); System.out.println(&apos; 4. Extra-Large Cheeze Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos;You ordered Onion Pizza&apos;); System.out.println(&apos;Enter the Onion pizza size&apos;); System.out.println(&apos;----------------------------------&apos;); System.out.println(&apos; 1. Small Onion Pizza &apos;); System.out.println(&apos; 2. Medium Onion Pizza &apos;); System.out.println(&apos; 3. Large Onion Pizza &apos;); System.out.println(&apos; 4. Extra-Large Onion Pizza &apos;); System.out.println(&apos;----------------------------------&apos;); 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(&apos;You ordered Masala Pizza&apos;); System.out.println(&apos;Enter the Masala pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Masala Pizza &apos;); System.out.println(&apos; 2. Medium Masala Pizza &apos;); System.out.println(&apos; 3. Large Masala Pizza &apos;); System.out.println(&apos; 4. Extra-Large Masala Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos;You ordered Non-Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos;Enter the Non-Veg pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Non-Veg Pizza &apos;); System.out.println(&apos; 2. Medium Non-Veg Pizza &apos;); System.out.println(&apos; 3. Large Non-Veg Pizza &apos;); System.out.println(&apos; 4. Extra-Large Non-Veg Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos; Enter the choice of ColdDrink &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Pepsi &apos;); System.out.println(&apos; 2. Coke &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println(&apos;You ordered Pepsi &apos;); System.out.println(&apos;Enter the Pepsi Size &apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Pepsi &apos;); System.out.println(&apos; 2. Medium Pepsi &apos;); System.out.println(&apos; 3. Large Pepsi &apos;); System.out.println(&apos;------------------------&apos;); 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(&apos;You ordered Coke&apos;); System.out.println(&apos;Enter the Coke Size&apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Coke &apos;); System.out.println(&apos; 2. Medium Coke &apos;); System.out.println(&apos; 3. Large Coke &apos;); System.out.println(&apos; 4. Extra-Large Coke &apos;); System.out.println(&apos;------------------------&apos;); 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(&apos;
&apos;); System.out.println(&apos;Total Cost : &apos;+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>  

14. korak: Ustvarite razred OrderBuilder, ki bo odgovoren za ustvarjanje objektov razreda OrderedItems.

Datoteka: OrdereBuilder.java
 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(&apos; Enter the choice of Pizza &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Veg-Pizza &apos;); System.out.println(&apos; 2. Non-Veg Pizza &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println(&apos;You ordered Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos; Enter the types of Veg-Pizza &apos;); System.out.println(&apos;------------------------------&apos;); System.out.println(&apos; 1.Cheeze Pizza &apos;); System.out.println(&apos; 2.Onion Pizza &apos;); System.out.println(&apos; 3.Masala Pizza &apos;); System.out.println(&apos; 4.Exit &apos;); System.out.println(&apos;------------------------------&apos;); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println(&apos;You ordered Cheeze Pizza&apos;); System.out.println(&apos;Enter the cheeze pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Cheeze Pizza &apos;); System.out.println(&apos; 2. Medium Cheeze Pizza &apos;); System.out.println(&apos; 3. Large Cheeze Pizza &apos;); System.out.println(&apos; 4. Extra-Large Cheeze Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos;You ordered Onion Pizza&apos;); System.out.println(&apos;Enter the Onion pizza size&apos;); System.out.println(&apos;----------------------------------&apos;); System.out.println(&apos; 1. Small Onion Pizza &apos;); System.out.println(&apos; 2. Medium Onion Pizza &apos;); System.out.println(&apos; 3. Large Onion Pizza &apos;); System.out.println(&apos; 4. Extra-Large Onion Pizza &apos;); System.out.println(&apos;----------------------------------&apos;); 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(&apos;You ordered Masala Pizza&apos;); System.out.println(&apos;Enter the Masala pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Masala Pizza &apos;); System.out.println(&apos; 2. Medium Masala Pizza &apos;); System.out.println(&apos; 3. Large Masala Pizza &apos;); System.out.println(&apos; 4. Extra-Large Masala Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos;You ordered Non-Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos;Enter the Non-Veg pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Non-Veg Pizza &apos;); System.out.println(&apos; 2. Medium Non-Veg Pizza &apos;); System.out.println(&apos; 3. Large Non-Veg Pizza &apos;); System.out.println(&apos; 4. Extra-Large Non-Veg Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); 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(&apos; Enter the choice of ColdDrink &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Pepsi &apos;); System.out.println(&apos; 2. Coke &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println(&apos;You ordered Pepsi &apos;); System.out.println(&apos;Enter the Pepsi Size &apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Pepsi &apos;); System.out.println(&apos; 2. Medium Pepsi &apos;); System.out.println(&apos; 3. Large Pepsi &apos;); System.out.println(&apos;------------------------&apos;); 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(&apos;You ordered Coke&apos;); System.out.println(&apos;Enter the Coke Size&apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Coke &apos;); System.out.println(&apos; 2. Medium Coke &apos;); System.out.println(&apos; 3. Large Coke &apos;); System.out.println(&apos; 4. Extra-Large Coke &apos;); System.out.println(&apos;------------------------&apos;); 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 

15. korak: Ustvarite razred BuilderDemo, ki bo uporabljal razred OrderBuilder.

Datoteka: Prototype.java
 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(&apos;
&apos;); System.out.println(&apos;Total Cost : &apos;+ orderedItems.getCost()); } }// End of the BuilderDemo class 

prenesite ta primer Builder Pattern

Izhod