logo

Шаблон за дизайн на конструктор

  1. Шаблон за дизайн на конструктор
  2. Предимство на Builder DP
  3. Използване на Builder DP
  4. UML на Builder DP
  5. Пример за Builder DP

Builder Pattern казва това „конструирайте сложен обект от прости обекти, използвайки подход стъпка по стъпка“

Използва се най-вече, когато обектът не може да бъде създаден в една стъпка, както при десериализацията на сложен обект.

Предимство на Builder Design Pattern

Основните предимства на Builder Pattern са следните:

  • Той осигурява ясно разделение между конструкцията и представянето на обект.
  • Осигурява по-добър контрол върху строителния процес.
  • Поддържа промяна на вътрешното представяне на обекти.

Пример за модел на UML за Builder


Пример за модел на дизайн на Builder

За да създадете прост пример за шаблон за проектиране на строител, трябва да следвате 6 стъпки по-долу.

  1. Създайте интерфейс за опаковане
  2. Създайте 2 абстрактни класа CD и Company
  3. Създайте 2 класа за изпълнение на компанията: Sony и Samsung
  4. Създайте класа CDType
  5. Създайте класа CDBuilder
  6. Създайте класа BuilderDemo

1) Създайте интерфейс за опаковане

Файл: Packing.java
 public interface Packing { public String pack(); public int price(); } 

2) Създайте 2 абстрактни класа CD и Company

Създайте CD с абстрактен клас, който ще имплементира пакетиращ интерфейс.

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

3) Създайте 2 класа за изпълнение на компанията: Sony и Samsung

Файл: 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. 
Файл: 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) Създайте класа CDType

Файл: 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) Създайте класа CDBuilder

Файл: 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) Създайте клас BuilderDemo

Файл: 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(); } } 

изтеглете този пример за модел на конструктор

Резултат от горния пример

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

Друг пример от реалния свят на Builder Pattern

Шаблон на UML за Builder:

Обмисляме бизнес случай на Pizza Hut където можем да вземем различни видове пица и студени напитки.

пица може да бъде вегетарианска пица или пица без зеленчуци от няколко вида (като пица със сирене, пица с лук, масала-пица и т.н.) и ще бъде в 4 размера, т.е. малка, средна, голяма, изключително голяма.

Студена напитка може да бъде от няколко вида (като Pepsi, Coke, Dew, Sprite, Fanta, Maaza, Limca, Thums-up и т.н.) и ще бъде от 3 размера малък, среден, голям.

Пример от реалния свят за модел на строител

Нека видим стъпка по стъпка примера от реалния свят на Builder Design Pattern.

Етап 1: Създайте интерфейсен елемент, който представлява пицата и студената напитка.

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

Стъпка 2: Създайте абстрактен клас Pizza, който ще имплементира към интерфейса Item.

Файл: Pizza.java
 public abstract class Pizza implements Item{ @Override public abstract float price(); } 

Стъпка 3: Създайте абстрактен клас ColdDrink, който ще имплементира към интерфейса Item.

Файл: ColdDrink.java
 public abstract class ColdDrink implements Item{ @Override public abstract float price(); } 

Стъпка 4: Създайте абстрактен клас VegPizza, който ще се разшири до абстрактния клас Pizza.

Файл: 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: Създайте абстрактен клас NonVegPizza, който ще се разшири до абстрактния клас Pizza.

Файл: 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: Сега създайте конкретни подкласове SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza, които ще се разширят до абстрактния клас VegPizza.

Файл: 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. 
Файл: 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>
Файл: 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: Сега създайте по подобен начин конкретни подкласове SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza, които ще се разширят до абстрактния клас VegPizza.

Файл: 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. 
Файл: 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. 
Файл: 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. 
Файл: 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: Сега създайте по подобен начин конкретни подкласове SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza, които ще се разширят до абстрактния клас VegPizza.

Файл: 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 
Файл: 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;; } 
Файл: 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 
Файл: 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: Сега създайте конкретни подкласове SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza, които ще се разширят до абстрактния клас NonVegPizza.

Файл: 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 
Файл: 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;; } 
Файл: 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 
Файл: 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: Сега създайте два абстрактни класа Pepsi и Coca-Cola, които ще разширят абстрактния клас ColdDrink.

Файл: 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 
Файл: 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>
Файл: 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 
Файл: 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: Сега създайте конкретни подкласове SmallCoke, MediumCoke, LargeCoke, които ще се разширят до абстрактния клас Coke.

Файл: 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 
Файл: 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 
Файл: 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: Създайте клас OrderBuilder, който ще отговаря за създаването на обектите от класа OrderedItems.

Файл: 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: Създайте клас BuilderDemo, който ще използва класа OrderBuilder.

Файл: 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 

изтеглете този пример за шаблон на Builder

Изход