ビルダーパターンは次のように述べています 「ステップバイステップのアプローチを使用して、単純なオブジェクトから複雑なオブジェクトを構築する」
これは主に、複雑なオブジェクトの逆シリアル化など、単一ステップでオブジェクトを作成できない場合に使用されます。
Builder デザイン パターンの利点
Builder パターンの主な利点は次のとおりです。
- これにより、オブジェクトの構築と表現が明確に分離されます。
- 建設プロセスをより適切に制御できます。
- オブジェクトの内部表現の変更をサポートします。
ビルダーパターンのUMLの例
ビルダーデザインパターンの例
ビルダー設計パターンの簡単な例を作成するには、次の 6 つの手順に従う必要があります。
- パッキングインターフェイスの作成
- CD と Company の 2 つの抽象クラスを作成します
- Company の 2 つの実装クラスを作成します: Sony と Samsung
- CDType クラスを作成する
- CDBuilder クラスを作成する
- BuilderDemoクラスを作成する
1) パッキングインターフェースの作成
ファイル: Packing.javapublic interface Packing { public String pack(); public int price(); }
2) CD と Company の 2 つの抽象クラスを作成します。
Packing インターフェイスを実装する抽象クラス CD を作成します。
ファイル: CD.javapublic abstract class CD implements Packing{ public abstract String pack(); }ファイル: Company.java
public abstract class Company extends CD{ public abstract int price(); }
3) 会社の 2 つの実装クラスを作成します: Sony と Samsung
ファイル: Sony.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:
というビジネスケースを検討しています。 ピザハット ここではさまざまな種類のピザと冷たい飲み物を買うことができます。
ピザ いくつかの種類(チーズピザ、オニオンピザ、マサラピザなど)のベジピザまたはノンベジピザのいずれかで、サイズは小、中、大、特大の4つあります。
冷たい飲み物 いくつかのタイプ (ペプシ、コーラ、デュー、スプライト、ファンタ、マーザ、リムカ、サムズアップなど) があり、サイズは小、中、大の 3 つあります。
ビルダー パターンの実世界の例
Builder デザイン パターンの実際の例を段階的に見てみましょう。
ステップ1: ピザと冷たい飲み物を表すインターフェイス項目を作成します。
ファイル:Item.javapublic interface Item { public String name(); public String size(); public float price(); }// End of the interface Item.
ステップ2: インターフェイスItemに実装する抽象クラスPizzaを作成します。
ファイル: Pizza.javapublic abstract class Pizza implements Item{ @Override public abstract float price(); }
ステップ 3: インターフェイスItemに実装する抽象クラスColdDrinkを作成します。
ファイル: ColdDrink.javapublic abstract class ColdDrink implements Item{ @Override public abstract float price(); }
ステップ 4: 抽象クラス Pizza を拡張する抽象クラス 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: 抽象クラス Pizza を拡張する抽象クラス NonVegPizza を作成します。
ファイル: 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: ここで、抽象クラス VegPizza に拡張する具体的なサブクラス SmallCheezePizza、MediumCheezePizza、LargeCheezePizza、ExtraLargeCheezePizza を作成します。
ファイル: 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: 次に、同様に、抽象クラス VegPizza に拡張される具象サブクラス SmallOnionPizza、MediumOnionPizza、LargeOnionPizza、ExtraLargeOnionPizza を作成します。
ファイル: 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: 次に、同様に、抽象クラス VegPizza に拡張される具象サブクラス SmallMasalaPizza、MediumMasalaPizza、LargeMasalaPizza、ExtraLargeMasalaPizza を作成します。
ファイル: 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: 次に、抽象クラス NonVegPizza に拡張する具体的なサブクラス SmallNonVegPizza、MediumNonVegPizza、LargeNonVegPizza、ExtraLargeNonVegPizza を作成します。
ファイル: 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: 次に、抽象クラス ColdDrink を拡張する 2 つの抽象クラス Pepsi と Coke を作成します。
ファイル: ペプシ.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: 次に、抽象クラス Coke に拡張する具象サブクラス SmallCoke、MediumCoke、LargeCoke を作成します。
ファイル: 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: OrderedItems クラスのオブジェクトの作成を担当する OrderBuilder クラスを作成します。
ファイル: 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: OrderBuilder クラスを使用する BuilderDemo クラスを作成します。
ファイル: 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
このビルダー パターンの例をダウンロード