يقول نمط البناء ذلك 'إنشاء كائن معقد من كائنات بسيطة باستخدام نهج خطوة بخطوة'
يتم استخدامه في الغالب عندما لا يمكن إنشاء الكائن في خطوة واحدة كما هو الحال في إلغاء تسلسل كائن معقد.
الاستفادة من نمط تصميم البناء
المزايا الرئيسية لنمط البناء هي كما يلي:
- يوفر فصلًا واضحًا بين بناء الجسم وتمثيله.
- يوفر تحكمًا أفضل في عملية البناء.
- وهو يدعم تغيير التمثيل الداخلي للكائنات.
UML لمثال نمط المنشئ
مثال على نمط تصميم البناء
لإنشاء مثال بسيط لنمط تصميم المنشئ، عليك اتباع 6 خطوات التالية.
- إنشاء واجهة التعبئة
- قم بإنشاء قرصين مضغوطين وشركة لفئات مجردة
- قم بإنشاء فئتين تنفيذيتين للشركة: Sony وSamsung
- قم بإنشاء فئة CDType
- قم بإنشاء فئة CDBuilder
- قم بإنشاء فئة BuilderDemo
1) إنشاء واجهة التعبئة
ملف:packing.javapublic interface Packing { public String pack(); public int price(); }
2) قم بإنشاء قرصين مضغوطين وشركة لفئات مجردة
قم بإنشاء قرص مضغوط للفئة المجردة والذي سيقوم بتنفيذ واجهة التعبئة.
الملف: CD.javapublic abstract class CD implements Packing{ public abstract String pack(); }الملف: Company.java
public abstract class Company extends CD{ public abstract int price(); }
3) إنشاء فئتين تنفيذيتين للشركة: Sony وSamsung
الملف: Sony.javapublic 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.javaimport java.util.ArrayList; import java.util.List; public class CDType { private List items=new ArrayList(); public void addItem(Packing packs) { items.add(packs); } public void getCost(){ for (Packing packs : items) { packs.price(); } } public void showItems(){ for (Packing packing : items){ System.out.print('CD name : '+packing.pack()); System.out.println(', Price : '+packing.price()); } } }//End of the CDType class.
5) قم بإنشاء فئة CDBuilder
الملف: CDBuilder.javapublic class CDBuilder { public CDType buildSonyCD(){ CDType cds=new CDType(); cds.addItem(new Sony()); return cds; } public CDType buildSamsungCD(){ CDType cds=new CDType(); cds.addItem(new Samsung()); return cds; } }// End of the CDBuilder class.
6) إنشاء فئة BuilderDemo
الملف: BuilderDemo.javapublic class BuilderDemo{ public static void main(String args[]){ CDBuilder cdBuilder=new CDBuilder(); CDType cdType1=cdBuilder.buildSonyCD(); cdType1.showItems(); CDType cdType2=cdBuilder.buildSamsungCD(); cdType2.showItems(); } }
قم بتنزيل مثال نمط البناء هذا
إخراج المثال أعلاه
CD name : Sony CD, Price : 20 CD name : Samsung CD, Price : 15
مثال آخر من العالم الحقيقي لنمط البناء
UML لنمط البناء:
نحن ندرس حالة تجارية ل بيتزا هت حيث يمكننا الحصول على أنواع مختلفة من البيتزا والمشروبات الباردة.
بيتزا يمكن أن تكون إما بيتزا نباتية أو بيتزا غير نباتية من عدة أنواع (مثل بيتزا الجبن، بيتزا البصل، بيتزا ماسالا، إلخ) وستكون بأربعة أحجام، أي صغير، متوسط، كبير، كبير جدًا.
ج# يحتوي على سلسلة
مشروب بارد يمكن أن تكون من عدة أنواع (مثل Pepsi، Coke، Dew، Sprite، Fanta، Maaza، Limca، Thums-up وما إلى ذلك) وستكون بثلاثة أحجام صغيرة ومتوسطة وكبيرة.
مثال عالمي حقيقي لنمط البناء
دعونا نرى مثالًا حقيقيًا خطوة بخطوة لنمط تصميم المنشئ.
الخطوة 1: قم بإنشاء عنصر واجهة يمثل البيتزا والمشروبات الباردة.
الملف: Item.javapublic interface Item { public String name(); public String size(); public float price(); }// End of the interface Item.
الخطوة 2: قم بإنشاء بيتزا فئة مجردة سيتم تنفيذها على عنصر الواجهة.
الملف: Pizza.javapublic abstract class Pizza implements Item{ @Override public abstract float price(); }
الخطوه 3: قم بإنشاء فئة مجردة ColdDrink والتي سيتم تطبيقها على عنصر الواجهة.
الملف: ColdDrink.javapublic abstract class ColdDrink implements Item{ @Override public abstract float price(); }
الخطوة 4: قم بإنشاء فئة مجردة VegPizza والتي ستمتد إلى فئة البيتزا المجردة.
الملف: VegPizza.javapublic abstract class VegPizza extends Pizza{ @Override public abstract float price(); @Override public abstract String name(); @Override public abstract String size(); }// End of the abstract class VegPizza.
الخطوة 5: قم بإنشاء فئة مجردة NonVegPizza والتي ستمتد إلى فئة مجردة Pizza.
الملف: NonVegPizza.javapublic abstract class NonVegPizza extends Pizza{ @Override public abstract float price(); @Override public abstract String name(); @Override public abstract String size(); }// End of the abstract class NonVegPizza.
الخطوة 6: الآن، قم بإنشاء فئات فرعية محددة SmallCheezePizza وMediumCheezePizza وLargeCheezePizza وExtraLargeCheezePizza والتي ستمتد إلى الفئة المجردة VegPizza.
الملف: SmallCheezePizza.javapublic class SmallCheezePizza extends VegPizza{ @Override public float price() { return 170.0f; } @Override public String name() { return 'Cheeze Pizza'; } @Override public String size() { return 'Small size'; } }// End of the SmallCheezePizza class.الملف: MediumCheezePizza.java
public class MediumCheezePizza extends VegPizza{ @Override public float price() { return 220.f; } @Override public String name() { return 'Cheeze Pizza'; } @Override public String size() { return 'Medium Size'; } }// End of the MediumCheezePizza class. File: LargeCheezePizza.java <pre> public class LargeCheezePizza extends VegPizza{ @Override public float price() { return 260.0f; } @Override public String name() { return 'Cheeze Pizza'; } @Override public String size() { return 'Large Size'; } }// End of the LargeCheezePizza class. </pre> File: ExtraLargeCheezePizza.java <pre> public class ExtraLargeCheezePizza extends VegPizza{ @Override public float price() { return 300.f; } @Override public String name() { return 'Cheeze Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeCheezePizza class. </pre> <p>Step 7: <b>Now, similarly create concrete sub-classes SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza that will extend to the abstract class VegPizza.</b> </p> File: SmallOnionPizza.java <pre> public class SmallOnionPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Small Size'; } }// End of the SmallOnionPizza class. </pre> File: MediumOnionPizza.java <pre> public class MediumOnionPizza extends VegPizza { @Override public float price() { return 150.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Medium Size'; } }// End of the MediumOnionPizza class. </pre> File: LargeOnionPizza.java <pre> public class LargeOnionPizza extends VegPizza{ @Override public float price() { return 180.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Large size'; } }// End of the LargeOnionPizza class. </pre> File: ExtraLargeOnionPizza.java <pre> public class ExtraLargeOnionPizza extends VegPizza { @Override public float price() { return 200.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeOnionPizza class </pre> <p>Step 8: <b>Now, similarly create concrete sub-classes SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza that will extend to the abstract class VegPizza.</b> </p> File: SmallMasalaPizza.java <pre> public class SmallMasalaPizza extends VegPizza{ @Override public float price() { return 100.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Samll Size'; } }// End of the SmallMasalaPizza class </pre> File: MediumMasalaPizza.java <pre> public class MediumMasalaPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Medium Size'; } </pre> File: LargeMasalaPizza.java <pre> public class LargeMasalaPizza extends VegPizza{ @Override public float price() { return 150.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Large Size'; } } //End of the LargeMasalaPizza class </pre> File: ExtraLargeMasalaPizza.java <pre> public class ExtraLargeMasalaPizza extends VegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeMasalaPizza class </pre> <p>Step 9: <b>Now, create concrete sub-classes SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza that will extend to the abstract class NonVegPizza.</b> </p> File: SmallNonVegPizza.java <pre> public class SmallNonVegPizza extends NonVegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Samll Size'; } }// End of the SmallNonVegPizza class </pre> File: MediumNonVegPizza.java <pre> public class MediumNonVegPizza extends NonVegPizza{ @Override public float price() { return 200.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Medium Size'; } </pre> File: LargeNonVegPizza.java <pre> public class LargeNonVegPizza extends NonVegPizza{ @Override public float price() { return 220.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Large Size'; } }// End of the LargeNonVegPizza class </pre> File: ExtraLargeNonVegPizza.java <pre> public class ExtraLargeNonVegPizza extends NonVegPizza { @Override public float price() { return 250.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Extra-Large Size'; } } // End of the ExtraLargeNonVegPizza class </pre> <p>Step 10: <b>Now, create two abstract classes Pepsi and Coke that will extend abstract class ColdDrink. </b> </p> File: Pepsi.java <pre> public abstract class Pepsi extends ColdDrink { @Override public abstract String name(); @Override public abstract String size(); @Override public abstract float price(); }// End of the Pepsi class </pre> File: Coke.java <pre> public abstract class Coke extends ColdDrink { @Override public abstract String name(); @Override public abstract String size(); @Override public abstract float price(); }// End of the Coke class <p>Step 11: <b>Now, create concrete sub-classes SmallPepsi, MediumPepsi, LargePepsi that will extend to the abstract class Pepsi.</b> </p> File: SmallPepsi.java <pre> public class SmallPepsi extends Pepsi{ @Override public String name() { return '300 ml Pepsi'; } @Override public float price() { return 25.0f; } @Override public String size() { return 'Small Size'; } }// End of the SmallPepsi class </pre> File: MediumPepsi.java <pre> public class MediumPepsi extends Pepsi { @Override public String name() { return '500 ml Pepsi'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi class </pre> File: LargePepsi.java <pre> public class LargePepsi extends Pepsi{ @Override public String name() { return '750 ml Pepsi'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class </pre> <p>Step 12: <b>Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke.</b> </p> File: SmallCoke.java <pre> public class SmallCoke extends Coke{ @Override public String name() { return '300 ml Coke'; } @Override public String size() { return 'Small Size'; } @Override public float price() { return 25.0f; } }// End of the SmallCoke class </pre> File: MediumCoke.java <pre> public class MediumCoke extends Coke{ @Override public String name() { return '500 ml Coke'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumCoke class </pre> File: LargeCoke.java <pre> public class LargeCoke extends Coke { @Override public String name() { return '750 ml Coke'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.println('Item is:' +item.name()); System.out.println('Size is:' +item.size()); System.out.println('Price is: ' +item.price()); } } }// End of the OrderedItems class </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(' Enter the choice of Pizza '); System.out.println('============================'); System.out.println(' 1. Veg-Pizza '); System.out.println(' 2. Non-Veg Pizza '); System.out.println(' 3. Exit '); System.out.println('============================'); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); System.out.println(' Enter the types of Veg-Pizza '); System.out.println('------------------------------'); System.out.println(' 1.Cheeze Pizza '); System.out.println(' 2.Onion Pizza '); System.out.println(' 3.Masala Pizza '); System.out.println(' 4.Exit '); System.out.println('------------------------------'); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println('You ordered Cheeze Pizza'); System.out.println('Enter the cheeze pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Cheeze Pizza '); System.out.println(' 2. Medium Cheeze Pizza '); System.out.println(' 3. Large Cheeze Pizza '); System.out.println(' 4. Extra-Large Cheeze Pizza '); System.out.println('------------------------------------'); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println('You ordered Onion Pizza'); System.out.println('Enter the Onion pizza size'); System.out.println('----------------------------------'); System.out.println(' 1. Small Onion Pizza '); System.out.println(' 2. Medium Onion Pizza '); System.out.println(' 3. Large Onion Pizza '); System.out.println(' 4. Extra-Large Onion Pizza '); System.out.println('----------------------------------'); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println('You ordered Masala Pizza'); System.out.println('Enter the Masala pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Masala Pizza '); System.out.println(' 2. Medium Masala Pizza '); System.out.println(' 3. Large Masala Pizza '); System.out.println(' 4. Extra-Large Masala Pizza '); System.out.println('------------------------------------'); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); System.out.println('Enter the Non-Veg pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Non-Veg Pizza '); System.out.println(' 2. Medium Non-Veg Pizza '); System.out.println(' 3. Large Non-Veg Pizza '); System.out.println(' 4. Extra-Large Non-Veg Pizza '); System.out.println('------------------------------------'); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(' Enter the choice of ColdDrink '); System.out.println('============================'); System.out.println(' 1. Pepsi '); System.out.println(' 2. Coke '); System.out.println(' 3. Exit '); System.out.println('============================'); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println('You ordered Pepsi '); System.out.println('Enter the Pepsi Size '); System.out.println('------------------------'); System.out.println(' 1. Small Pepsi '); System.out.println(' 2. Medium Pepsi '); System.out.println(' 3. Large Pepsi '); System.out.println('------------------------'); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println('You ordered Coke'); System.out.println('Enter the Coke Size'); System.out.println('------------------------'); System.out.println(' 1. Small Coke '); System.out.println(' 2. Medium Coke '); System.out.println(' 3. Large Coke '); System.out.println(' 4. Extra-Large Coke '); System.out.println('------------------------'); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4> </pre></pre>الملف: ExtraLargeCheezePizza.java
public class ExtraLargeCheezePizza extends VegPizza{ @Override public float price() { return 300.f; } @Override public String name() { return 'Cheeze Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeCheezePizza class.
الخطوة 7: الآن، قم أيضًا بإنشاء فئات فرعية ملموسة SmallOnionPizza وMediumOnionPizza وLargeOnionPizza وExtraLargeOnionPizza والتي ستمتد إلى الفئة المجردة VegPizza.
الملف: SmallOnionPizza.javapublic class SmallOnionPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Small Size'; } }// End of the SmallOnionPizza class.الملف: MediumOnionPizza.java
public class MediumOnionPizza extends VegPizza { @Override public float price() { return 150.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Medium Size'; } }// End of the MediumOnionPizza class.الملف: LargeOnionPizza.java
public class LargeOnionPizza extends VegPizza{ @Override public float price() { return 180.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Large size'; } }// End of the LargeOnionPizza class.الملف: ExtraLargeOnionPizza.java
public class ExtraLargeOnionPizza extends VegPizza { @Override public float price() { return 200.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeOnionPizza class
الخطوة 8: الآن، قم أيضًا بإنشاء فئات فرعية ملموسة SmallMasalaPizza وMediumMasalaPizza وLargeMasalaPizza وExtraLargeMasalaPizza والتي ستمتد إلى الفئة المجردة VegPizza.
الملف: SmallMasalaPizza.javapublic class SmallMasalaPizza extends VegPizza{ @Override public float price() { return 100.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Samll Size'; } }// End of the SmallMasalaPizza classالملف: MediumMasalaPizza.java
public class MediumMasalaPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Medium Size'; }الملف: LargeMasalaPizza.java
public class LargeMasalaPizza extends VegPizza{ @Override public float price() { return 150.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Large Size'; } } //End of the LargeMasalaPizza classالملف: ExtraLargeMasalaPizza.java
public class ExtraLargeMasalaPizza extends VegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeMasalaPizza class
الخطوة 9: الآن، قم بإنشاء فئات فرعية ملموسة SmallNonVegPizza وMediumNonVegPizza وLargeNonVegPizza وExtraLargeNonVegPizza والتي ستمتد إلى الفئة المجردة NonVegPizza.
الملف: SmallNonVegPizza.javapublic class SmallNonVegPizza extends NonVegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Samll Size'; } }// End of the SmallNonVegPizza classالملف: MediumNonVegPizza.java
public class MediumNonVegPizza extends NonVegPizza{ @Override public float price() { return 200.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Medium Size'; }الملف: LargeNonVegPizza.java
public class LargeNonVegPizza extends NonVegPizza{ @Override public float price() { return 220.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Large Size'; } }// End of the LargeNonVegPizza classالملف: ExtraLargeNonVegPizza.java
public class ExtraLargeNonVegPizza extends NonVegPizza { @Override public float price() { return 250.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Extra-Large Size'; } } // End of the ExtraLargeNonVegPizza class
الخطوة 10: الآن، قم بإنشاء فئتين تجريديتين Pepsi وCoke من شأنها توسيع فئة ColdDrink المجردة.
الملف: Pepsi.javapublic abstract class Pepsi extends ColdDrink { @Override public abstract String name(); @Override public abstract String size(); @Override public abstract float price(); }// End of the Pepsi 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 '300 ml Pepsi'; } @Override public float price() { return 25.0f; } @Override public String size() { return 'Small Size'; } }// End of the SmallPepsi class </pre> File: MediumPepsi.java <pre> public class MediumPepsi extends Pepsi { @Override public String name() { return '500 ml Pepsi'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi class </pre> File: LargePepsi.java <pre> public class LargePepsi extends Pepsi{ @Override public String name() { return '750 ml Pepsi'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class </pre> <p>Step 12: <b>Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke.</b> </p> File: SmallCoke.java <pre> public class SmallCoke extends Coke{ @Override public String name() { return '300 ml Coke'; } @Override public String size() { return 'Small Size'; } @Override public float price() { return 25.0f; } }// End of the SmallCoke class </pre> File: MediumCoke.java <pre> public class MediumCoke extends Coke{ @Override public String name() { return '500 ml Coke'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumCoke class </pre> File: LargeCoke.java <pre> public class LargeCoke extends Coke { @Override public String name() { return '750 ml Coke'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.println('Item is:' +item.name()); System.out.println('Size is:' +item.size()); System.out.println('Price is: ' +item.price()); } } }// End of the OrderedItems class </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(' Enter the choice of Pizza '); System.out.println('============================'); System.out.println(' 1. Veg-Pizza '); System.out.println(' 2. Non-Veg Pizza '); System.out.println(' 3. Exit '); System.out.println('============================'); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); System.out.println(' Enter the types of Veg-Pizza '); System.out.println('------------------------------'); System.out.println(' 1.Cheeze Pizza '); System.out.println(' 2.Onion Pizza '); System.out.println(' 3.Masala Pizza '); System.out.println(' 4.Exit '); System.out.println('------------------------------'); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println('You ordered Cheeze Pizza'); System.out.println('Enter the cheeze pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Cheeze Pizza '); System.out.println(' 2. Medium Cheeze Pizza '); System.out.println(' 3. Large Cheeze Pizza '); System.out.println(' 4. Extra-Large Cheeze Pizza '); System.out.println('------------------------------------'); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println('You ordered Onion Pizza'); System.out.println('Enter the Onion pizza size'); System.out.println('----------------------------------'); System.out.println(' 1. Small Onion Pizza '); System.out.println(' 2. Medium Onion Pizza '); System.out.println(' 3. Large Onion Pizza '); System.out.println(' 4. Extra-Large Onion Pizza '); System.out.println('----------------------------------'); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println('You ordered Masala Pizza'); System.out.println('Enter the Masala pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Masala Pizza '); System.out.println(' 2. Medium Masala Pizza '); System.out.println(' 3. Large Masala Pizza '); System.out.println(' 4. Extra-Large Masala Pizza '); System.out.println('------------------------------------'); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); System.out.println('Enter the Non-Veg pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Non-Veg Pizza '); System.out.println(' 2. Medium Non-Veg Pizza '); System.out.println(' 3. Large Non-Veg Pizza '); System.out.println(' 4. Extra-Large Non-Veg Pizza '); System.out.println('------------------------------------'); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(' Enter the choice of ColdDrink '); System.out.println('============================'); System.out.println(' 1. Pepsi '); System.out.println(' 2. Coke '); System.out.println(' 3. Exit '); System.out.println('============================'); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println('You ordered Pepsi '); System.out.println('Enter the Pepsi Size '); System.out.println('------------------------'); System.out.println(' 1. Small Pepsi '); System.out.println(' 2. Medium Pepsi '); System.out.println(' 3. Large Pepsi '); System.out.println('------------------------'); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println('You ordered Coke'); System.out.println('Enter the Coke Size'); System.out.println('------------------------'); System.out.println(' 1. Small Coke '); System.out.println(' 2. Medium Coke '); System.out.println(' 3. Large Coke '); System.out.println(' 4. Extra-Large Coke '); System.out.println('------------------------'); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4> </pre>الملف: MediumPepsi.java
public class MediumPepsi extends Pepsi { @Override public String name() { return '500 ml Pepsi'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi classالملف: LargePepsi.java
public class LargePepsi extends Pepsi{ @Override public String name() { return '750 ml Pepsi'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class
الخطوة 12: الآن، قم بإنشاء فئات فرعية ملموسة SmallCoke وMediumCoke وLargeCoke والتي ستمتد إلى الفئة المجردة Coke.
الملف: SmallCoke.javapublic class SmallCoke extends Coke{ @Override public String name() { return '300 ml Coke'; } @Override public String size() { return 'Small Size'; } @Override public float price() { return 25.0f; } }// End of the SmallCoke classالملف: MediumCoke.java
public class MediumCoke extends Coke{ @Override public String name() { return '500 ml Coke'; } @Override public String size() { return 'Medium Size'; } @Override public float price() { return 35.0f; } }// End of the MediumCoke classالملف: LargeCoke.java
public class LargeCoke extends Coke { @Override public String name() { return '750 ml Coke'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.println('Item is:' +item.name()); System.out.println('Size is:' +item.size()); System.out.println('Price is: ' +item.price()); } } }// End of the OrderedItems class </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(' Enter the choice of Pizza '); System.out.println('============================'); System.out.println(' 1. Veg-Pizza '); System.out.println(' 2. Non-Veg Pizza '); System.out.println(' 3. Exit '); System.out.println('============================'); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); System.out.println(' Enter the types of Veg-Pizza '); System.out.println('------------------------------'); System.out.println(' 1.Cheeze Pizza '); System.out.println(' 2.Onion Pizza '); System.out.println(' 3.Masala Pizza '); System.out.println(' 4.Exit '); System.out.println('------------------------------'); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println('You ordered Cheeze Pizza'); System.out.println('Enter the cheeze pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Cheeze Pizza '); System.out.println(' 2. Medium Cheeze Pizza '); System.out.println(' 3. Large Cheeze Pizza '); System.out.println(' 4. Extra-Large Cheeze Pizza '); System.out.println('------------------------------------'); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println('You ordered Onion Pizza'); System.out.println('Enter the Onion pizza size'); System.out.println('----------------------------------'); System.out.println(' 1. Small Onion Pizza '); System.out.println(' 2. Medium Onion Pizza '); System.out.println(' 3. Large Onion Pizza '); System.out.println(' 4. Extra-Large Onion Pizza '); System.out.println('----------------------------------'); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println('You ordered Masala Pizza'); System.out.println('Enter the Masala pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Masala Pizza '); System.out.println(' 2. Medium Masala Pizza '); System.out.println(' 3. Large Masala Pizza '); System.out.println(' 4. Extra-Large Masala Pizza '); System.out.println('------------------------------------'); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); System.out.println('Enter the Non-Veg pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Non-Veg Pizza '); System.out.println(' 2. Medium Non-Veg Pizza '); System.out.println(' 3. Large Non-Veg Pizza '); System.out.println(' 4. Extra-Large Non-Veg Pizza '); System.out.println('------------------------------------'); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(' Enter the choice of ColdDrink '); System.out.println('============================'); System.out.println(' 1. Pepsi '); System.out.println(' 2. Coke '); System.out.println(' 3. Exit '); System.out.println('============================'); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println('You ordered Pepsi '); System.out.println('Enter the Pepsi Size '); System.out.println('------------------------'); System.out.println(' 1. Small Pepsi '); System.out.println(' 2. Medium Pepsi '); System.out.println(' 3. Large Pepsi '); System.out.println('------------------------'); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println('You ordered Coke'); System.out.println('Enter the Coke Size'); System.out.println('------------------------'); System.out.println(' 1. Small Coke '); System.out.println(' 2. Medium Coke '); System.out.println(' 3. Large Coke '); System.out.println(' 4. Extra-Large Coke '); System.out.println('------------------------'); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>
الخطوة 14: قم بإنشاء فئة OrderBuilder التي ستكون مسؤولة عن إنشاء كائنات فئة OrderedItems.
الملف: OrdereBuilder.javaimport java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(' Enter the choice of Pizza '); System.out.println('============================'); System.out.println(' 1. Veg-Pizza '); System.out.println(' 2. Non-Veg Pizza '); System.out.println(' 3. Exit '); System.out.println('============================'); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); System.out.println(' Enter the types of Veg-Pizza '); System.out.println('------------------------------'); System.out.println(' 1.Cheeze Pizza '); System.out.println(' 2.Onion Pizza '); System.out.println(' 3.Masala Pizza '); System.out.println(' 4.Exit '); System.out.println('------------------------------'); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println('You ordered Cheeze Pizza'); System.out.println('Enter the cheeze pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Cheeze Pizza '); System.out.println(' 2. Medium Cheeze Pizza '); System.out.println(' 3. Large Cheeze Pizza '); System.out.println(' 4. Extra-Large Cheeze Pizza '); System.out.println('------------------------------------'); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println('You ordered Onion Pizza'); System.out.println('Enter the Onion pizza size'); System.out.println('----------------------------------'); System.out.println(' 1. Small Onion Pizza '); System.out.println(' 2. Medium Onion Pizza '); System.out.println(' 3. Large Onion Pizza '); System.out.println(' 4. Extra-Large Onion Pizza '); System.out.println('----------------------------------'); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println('You ordered Masala Pizza'); System.out.println('Enter the Masala pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Masala Pizza '); System.out.println(' 2. Medium Masala Pizza '); System.out.println(' 3. Large Masala Pizza '); System.out.println(' 4. Extra-Large Masala Pizza '); System.out.println('------------------------------------'); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); System.out.println('Enter the Non-Veg pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Non-Veg Pizza '); System.out.println(' 2. Medium Non-Veg Pizza '); System.out.println(' 3. Large Non-Veg Pizza '); System.out.println(' 4. Extra-Large Non-Veg Pizza '); System.out.println('------------------------------------'); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(' Enter the choice of ColdDrink '); System.out.println('============================'); System.out.println(' 1. Pepsi '); System.out.println(' 2. Coke '); System.out.println(' 3. Exit '); System.out.println('============================'); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println('You ordered Pepsi '); System.out.println('Enter the Pepsi Size '); System.out.println('------------------------'); System.out.println(' 1. Small Pepsi '); System.out.println(' 2. Medium Pepsi '); System.out.println(' 3. Large Pepsi '); System.out.println('------------------------'); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println('You ordered Coke'); System.out.println('Enter the Coke Size'); System.out.println('------------------------'); System.out.println(' 1. Small Coke '); System.out.println(' 2. Medium Coke '); System.out.println(' 3. Large Coke '); System.out.println(' 4. Extra-Large Coke '); System.out.println('------------------------'); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method
الخطوة 15: قم بإنشاء فئة BuilderDemo التي ستستخدم فئة OrderBuilder.
الملف: Prototype.javaimport java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class
قم بتنزيل مثال نمط البناء هذا