logo

דפוס עיצוב בונה

  1. דפוס עיצוב בונה
  2. יתרון של Builder DP
  3. שימוש ב-Builder DP
  4. UML של Builder DP
  5. דוגמה של Builder DP

Builder Pattern אומר את זה 'בנה אובייקט מורכב מאובייקטים פשוטים תוך שימוש בגישה שלב אחר שלב'

הוא משמש בעיקר כאשר לא ניתן ליצור אובייקט בשלב אחד כמו בדה-סריאליזציה של אובייקט מורכב.

היתרון של דפוס עיצוב Builder

היתרונות העיקריים של דפוס Builder הם כדלקמן:

  • הוא מספק הפרדה ברורה בין הבנייה והייצוג של אובייקט.
  • זה מספק שליטה טובה יותר על תהליך הבנייה.
  • זה תומך בשינוי הייצוג הפנימי של אובייקטים.

UML עבור דוגמה של Builder Pattern


דוגמה לתבנית עיצוב בונה

כדי ליצור דוגמה פשוטה של ​​דפוס עיצוב בונה, עליך לבצע 6 שלבים הבאים.

מיון הכנסה ב-java
  1. צור ממשק אריזה
  2. צור 2 שיעורים מופשטים CD וחברה
  3. צור 2 מחלקות יישום של חברה: סוני וסמסונג
  4. צור את המחלקה CDType
  5. צור את המחלקה CDBuilder
  6. צור את המחלקה BuilderDemo

1) צור ממשק אריזה

קובץ: Packing.java
 public interface Packing { public String pack(); public int price(); } 

2) צור 2 כיתות מופשטות 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.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:

אנו שוקלים מקרה עסקי של פיצה האט שבו נוכל להשיג סוגים שונים של פיצה ושתייה קרה.

פיצה יכולה להיות פיצה Veg או Non Veg מכמה סוגים (כמו פיצה גבינה, פיצה בצל, מסאלה-פיצה וכו') והיא תהיה ב-4 גדלים כלומר קטן, בינוני, גדול, גדול במיוחד.

אינדקס java של

משקה קר יכול להיות מכמה סוגים (כמו פפסי, קולה, טל, ספרייט, פנטה, מעזה, 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.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 שתתרחב לכיתה המופשטת פיצה.

קובץ: 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 שתתרחב לכיתה המופשטת פיצה.

קובץ: 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: כעת, צור שני מחלקות מופשטות פפסי וקולה שירחיבו את הכיתה המופשטת 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 Pattern

תְפוּקָה