- Wzór projektowy konstruktora
- Zalety Buildera DP
- Korzystanie z Buildera DP
- UML Buildera DP
- Przykład Buildera DP
Wzór konstruktora tak mówi „zbuduj złożony obiekt z prostych obiektów, stosując podejście krok po kroku”
Używa się go najczęściej, gdy obiektu nie można utworzyć w jednym kroku, jak w przypadku deserializacji złożonego obiektu.
Zaleta wzorca projektowego Builder
Główne zalety Builder Pattern są następujące:
obiekt tablicy w Javie
- Zapewnia wyraźne oddzielenie konstrukcji od przedstawienia obiektu.
- Zapewnia lepszą kontrolę nad procesem budowy.
- Obsługuje zmianę wewnętrznej reprezentacji obiektów.
UML dla przykładu wzorca konstruktora
Przykład wzorca projektowego Buildera
Aby stworzyć prosty przykład wzorca projektowego konstruktora, należy wykonać 6 następujących kroków.
- Utwórz interfejs pakowania
- Utwórz 2 klasy abstrakcyjne CD i Company
- Utwórz 2 klasy wdrożeń firmy: Sony i Samsung
- Utwórz klasę CDType
- Utwórz klasę CDBuilder
- Utwórz klasę BuilderDemo
1) Utwórz interfejs pakowania
Plik: Pakowanie.javapublic interface Packing { public String pack(); public int price(); }
2) Utwórz 2 klasy abstrakcyjne CD i Company
Utwórz płytę CD z klasą abstrakcyjną, która będzie implementować interfejs pakowania.
Plik: CD.javapublic abstract class CD implements Packing{ public abstract String pack(); }Plik: Company.java
public abstract class Company extends CD{ public abstract int price(); }
3) Utworzyć 2 klasy wdrożeń firmy: Sony i Samsung
Plik: Sony.javapublic class Sony extends Company{ @Override public int price(){ return 20; } @Override public String pack(){ return 'Sony CD'; } }//End of the Sony class.Plik: 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) Utwórz klasę CDType
Plik: 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) Utwórz klasę CDBuilder
Plik: 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) Utwórz klasę BuilderDemo
Plik: 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(); } }
pobierz ten przykładowy wzór konstruktora
Dane wyjściowe powyższego przykładu
CD name : Sony CD, Price : 20 CD name : Samsung CD, Price : 15
Kolejny przykład wzorca konstruktora z prawdziwego świata
UML dla wzorca konstruktora:
Rozważamy uzasadnienie biznesowe pizzeria gdzie możemy dostać różne rodzaje pizzy i zimne napoje.
Pizza może to być pizza wegetariańska lub pizza niewegetariańska kilku rodzajów (np. pizza z serem, pizza cebulowa, pizza masala itp.) i będzie dostępna w 4 rozmiarach, tj. małym, średnim, dużym i bardzo dużym.
Zimny napój może być kilku typów (np. Pepsi, Coca-Cola, Dew, Sprite, Fanta, Maaza, Limca, Thums-up itp.) i będzie dostępny w 3 rozmiarach: małym, średnim i dużym.
Przykład wzorca budowniczego z prawdziwego świata
Zobaczmy krok po kroku przykład wzorca projektowego Builder ze świata rzeczywistego.
Krok 1: Utwórz element interfejsu reprezentujący pizzę i zimny napój.
Plik: Pozycja.javapublic interface Item { public String name(); public String size(); public float price(); }// End of the interface Item.
Krok 2: Utwórz abstrakcyjną klasę Pizza, która zostanie zaimplementowana w interfejsie Item.
Plik: Pizza.javapublic abstract class Pizza implements Item{ @Override public abstract float price(); }
Krok 3: Utwórz klasę abstrakcyjną ColdDrink, która zostanie zaimplementowana w interfejsie Item.
Plik: ColdDrink.javapublic abstract class ColdDrink implements Item{ @Override public abstract float price(); }
Krok 4: Utwórz klasę abstrakcyjną VegPizza, która będzie obejmować klasę abstrakcyjną Pizza.
dzielenie ciągów C++Plik: 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.
Krok 5: Utwórz klasę abstrakcyjną NonVegPizza, która będzie obejmować klasę abstrakcyjną Pizza.
Plik: NonVegPizza.javapublic abstract class NonVegPizza extends Pizza{ @Override public abstract float price(); @Override public abstract String name(); @Override public abstract String size(); }// End of the abstract class NonVegPizza.
Krok 6: Teraz utwórz konkretne podklasy SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza, które rozszerzą się na klasę abstrakcyjną VegPizza.
Plik: 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.Plik: 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>Plik: ExtraLargeCheezePizza.java
public class ExtraLargeCheezePizza extends VegPizza{ @Override public float price() { return 300.f; } @Override public String name() { return 'Cheeze Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeCheezePizza class.
Krok 7: Teraz w podobny sposób utwórz konkretne podklasy SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza, które rozszerzą się na klasę abstrakcyjną VegPizza.
Plik: 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.Plik: 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.Plik: 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.Plik: ExtraLargeOnionPizza.java
public class ExtraLargeOnionPizza extends VegPizza { @Override public float price() { return 200.0f; } @Override public String name() { return 'Onion Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeOnionPizza class
Krok 8: Teraz w podobny sposób utwórz konkretne podklasy SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza, które rozszerzą się na klasę abstrakcyjną VegPizza.
Plik: 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 classPlik: 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'; }Plik: 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 classPlik: ExtraLargeMasalaPizza.java
public class ExtraLargeMasalaPizza extends VegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return 'Masala Pizza'; } @Override public String size() { return 'Extra-Large Size'; } }// End of the ExtraLargeMasalaPizza class
Krok 9: Teraz utwórz konkretne podklasy SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza, które rozszerzą się na klasę abstrakcyjną NonVegPizza.
Plik: 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 classPlik: 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'; }Plik: 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 classPlik: ExtraLargeNonVegPizza.java
public class ExtraLargeNonVegPizza extends NonVegPizza { @Override public float price() { return 250.0f; } @Override public String name() { return 'Non-Veg Pizza'; } @Override public String size() { return 'Extra-Large Size'; } } // End of the ExtraLargeNonVegPizza class
Krok 10: Teraz utwórz dwie klasy abstrakcyjne Pepsi i Coke, które będą stanowić rozszerzenie klasy abstrakcyjnej ColdDrink.
skaner.next JavaPlik: 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 classPlik: 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>Plik: 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 classPlik: LargePepsi.java
public class LargePepsi extends Pepsi{ @Override public String name() { return '750 ml Pepsi'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class
Krok 12: Teraz utwórz konkretne podklasy SmallCoke, MediumCoke, LargeCoke, które rozszerzą się na klasę abstrakcyjną Coke.
Plik: 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 classPlik: 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 classPlik: LargeCoke.java
public class LargeCoke extends Coke { @Override public String name() { return '750 ml Coke'; } @Override public String size() { return 'Large Size'; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.println('Item is:' +item.name()); System.out.println('Size is:' +item.size()); System.out.println('Price is: ' +item.price()); } } }// End of the OrderedItems class </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(' Enter the choice of Pizza '); System.out.println('============================'); System.out.println(' 1. Veg-Pizza '); System.out.println(' 2. Non-Veg Pizza '); System.out.println(' 3. Exit '); System.out.println('============================'); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); System.out.println(' Enter the types of Veg-Pizza '); System.out.println('------------------------------'); System.out.println(' 1.Cheeze Pizza '); System.out.println(' 2.Onion Pizza '); System.out.println(' 3.Masala Pizza '); System.out.println(' 4.Exit '); System.out.println('------------------------------'); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println('You ordered Cheeze Pizza'); System.out.println('Enter the cheeze pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Cheeze Pizza '); System.out.println(' 2. Medium Cheeze Pizza '); System.out.println(' 3. Large Cheeze Pizza '); System.out.println(' 4. Extra-Large Cheeze Pizza '); System.out.println('------------------------------------'); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println('You ordered Onion Pizza'); System.out.println('Enter the Onion pizza size'); System.out.println('----------------------------------'); System.out.println(' 1. Small Onion Pizza '); System.out.println(' 2. Medium Onion Pizza '); System.out.println(' 3. Large Onion Pizza '); System.out.println(' 4. Extra-Large Onion Pizza '); System.out.println('----------------------------------'); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println('You ordered Masala Pizza'); System.out.println('Enter the Masala pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Masala Pizza '); System.out.println(' 2. Medium Masala Pizza '); System.out.println(' 3. Large Masala Pizza '); System.out.println(' 4. Extra-Large Masala Pizza '); System.out.println('------------------------------------'); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); System.out.println('Enter the Non-Veg pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Non-Veg Pizza '); System.out.println(' 2. Medium Non-Veg Pizza '); System.out.println(' 3. Large Non-Veg Pizza '); System.out.println(' 4. Extra-Large Non-Veg Pizza '); System.out.println('------------------------------------'); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(' Enter the choice of ColdDrink '); System.out.println('============================'); System.out.println(' 1. Pepsi '); System.out.println(' 2. Coke '); System.out.println(' 3. Exit '); System.out.println('============================'); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println('You ordered Pepsi '); System.out.println('Enter the Pepsi Size '); System.out.println('------------------------'); System.out.println(' 1. Small Pepsi '); System.out.println(' 2. Medium Pepsi '); System.out.println(' 3. Large Pepsi '); System.out.println('------------------------'); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println('You ordered Coke'); System.out.println('Enter the Coke Size'); System.out.println('------------------------'); System.out.println(' 1. Small Coke '); System.out.println(' 2. Medium Coke '); System.out.println(' 3. Large Coke '); System.out.println(' 4. Extra-Large Coke '); System.out.println('------------------------'); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(' '); System.out.println('Total Cost : '+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>
Krok 14: Utwórz klasę OrderBuilder, która będzie odpowiedzialna za tworzenie obiektów klasy OrderedItems.
Plik: OrdereBuilder.javaimport java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(' Enter the choice of Pizza '); System.out.println('============================'); System.out.println(' 1. Veg-Pizza '); System.out.println(' 2. Non-Veg Pizza '); System.out.println(' 3. Exit '); System.out.println('============================'); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println('You ordered Veg Pizza'); System.out.println(' '); System.out.println(' Enter the types of Veg-Pizza '); System.out.println('------------------------------'); System.out.println(' 1.Cheeze Pizza '); System.out.println(' 2.Onion Pizza '); System.out.println(' 3.Masala Pizza '); System.out.println(' 4.Exit '); System.out.println('------------------------------'); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println('You ordered Cheeze Pizza'); System.out.println('Enter the cheeze pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Cheeze Pizza '); System.out.println(' 2. Medium Cheeze Pizza '); System.out.println(' 3. Large Cheeze Pizza '); System.out.println(' 4. Extra-Large Cheeze Pizza '); System.out.println('------------------------------------'); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println('You ordered Onion Pizza'); System.out.println('Enter the Onion pizza size'); System.out.println('----------------------------------'); System.out.println(' 1. Small Onion Pizza '); System.out.println(' 2. Medium Onion Pizza '); System.out.println(' 3. Large Onion Pizza '); System.out.println(' 4. Extra-Large Onion Pizza '); System.out.println('----------------------------------'); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println('You ordered Masala Pizza'); System.out.println('Enter the Masala pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Masala Pizza '); System.out.println(' 2. Medium Masala Pizza '); System.out.println(' 3. Large Masala Pizza '); System.out.println(' 4. Extra-Large Masala Pizza '); System.out.println('------------------------------------'); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println('You ordered Non-Veg Pizza'); System.out.println(' '); System.out.println('Enter the Non-Veg pizza size'); System.out.println('------------------------------------'); System.out.println(' 1. Small Non-Veg Pizza '); System.out.println(' 2. Medium Non-Veg Pizza '); System.out.println(' 3. Large Non-Veg Pizza '); System.out.println(' 4. Extra-Large Non-Veg Pizza '); System.out.println('------------------------------------'); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(' Enter the choice of ColdDrink '); System.out.println('============================'); System.out.println(' 1. Pepsi '); System.out.println(' 2. Coke '); System.out.println(' 3. Exit '); System.out.println('============================'); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println('You ordered Pepsi '); System.out.println('Enter the Pepsi Size '); System.out.println('------------------------'); System.out.println(' 1. Small Pepsi '); System.out.println(' 2. Medium Pepsi '); System.out.println(' 3. Large Pepsi '); System.out.println('------------------------'); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println('You ordered Coke'); System.out.println('Enter the Coke Size'); System.out.println('------------------------'); System.out.println(' 1. Small Coke '); System.out.println(' 2. Medium Coke '); System.out.println(' 3. Large Coke '); System.out.println(' 4. Extra-Large Coke '); System.out.println('------------------------'); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method
Krok 15: Utwórz klasę BuilderDemo, która będzie używać klasy OrderBuilder.
Plik: 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
pobierz ten przykładowy wzór konstruktora