logo

빌더 디자인 패턴

  1. 빌더 디자인 패턴
  2. 빌더 DP의 장점
  3. 빌더 DP 사용
  4. Builder DP의 UML
  5. 빌더 DP의 예

빌더 패턴은 다음과 같이 말합니다. '단계별 접근 방식을 사용하여 간단한 개체에서 복잡한 개체를 구성합니다.'

복잡한 객체의 역직렬화처럼 단일 단계로 객체를 생성할 수 없는 경우에 주로 사용됩니다.

Builder 디자인 패턴의 장점

빌더 패턴의 주요 장점은 다음과 같습니다.

  • 이는 객체의 구성과 표현을 명확하게 분리합니다.
  • 이는 건설 프로세스에 대한 더 나은 제어를 제공합니다.
  • 객체의 내부 표현을 변경하는 것을 지원합니다.

빌더 패턴 예를 위한 UML


빌더 디자인 패턴의 예

빌더 디자인 패턴의 간단한 예를 만들려면 다음 6단계를 수행해야 합니다.

Excel에서 첫 번째 문자 제거
  1. 패킹 인터페이스 생성
  2. 2개의 추상 클래스 CD 및 회사 만들기
  3. 회사의 구현 클래스 2개 생성: Sony 및 Samsung
  4. CDType 클래스 만들기
  5. CDBuilder 클래스 생성
  6. BuilderDemo 클래스 생성

1) 패킹 인터페이스 생성

파일:Packing.java
 public interface Packing { public String pack(); public int price(); } 

2) 2개의 추상클래스 CD와 Company를 생성합니다.

Packing 인터페이스를 구현할 추상 클래스 CD를 만듭니다.

파일: CD.java
 public abstract class CD implements Packing{ public abstract String pack(); } 
파일: Company.java
 public abstract class Company extends CD{ public abstract int price(); } 

3) 회사의 두 가지 구현 클래스 생성: Sony 및 Samsung

파일: Sony.java
 public class Sony extends Company{ @Override public int price(){ return 20; } @Override public String pack(){ return 'Sony CD'; } }//End of the Sony class. 
파일: Samsung.java
 public class Samsung extends Company { @Override public int price(){ return 15; } @Override public String pack(){ return 'Samsung CD'; } }//End of the Samsung class. 

4) CDType 클래스 생성

파일: CDType.java
 import java.util.ArrayList; import java.util.List; public class CDType { private List items=new ArrayList(); public void addItem(Packing packs) { items.add(packs); } public void getCost(){ for (Packing packs : items) { packs.price(); } } public void showItems(){ for (Packing packing : items){ System.out.print('CD name : '+packing.pack()); System.out.println(', Price : '+packing.price()); } } }//End of the CDType class. 

5) CDBuilder 클래스 생성

파일: CDBuilder.java
 public class CDBuilder { public CDType buildSonyCD(){ CDType cds=new CDType(); cds.addItem(new Sony()); return cds; } public CDType buildSamsungCD(){ CDType cds=new CDType(); cds.addItem(new Samsung()); return cds; } }// End of the CDBuilder class. 

6) BuilderDemo 클래스 생성

파일: BuilderDemo.java
 public class BuilderDemo{ public static void main(String args[]){ CDBuilder cdBuilder=new CDBuilder(); CDType cdType1=cdBuilder.buildSonyCD(); cdType1.showItems(); CDType cdType2=cdBuilder.buildSamsungCD(); cdType2.showItems(); } } 

이 빌더 패턴 예제를 다운로드하세요

위 예제의 출력

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

빌더 패턴의 또 다른 실제 사례

빌더 패턴용 UML:

우리는 다음과 같은 비즈니스 사례를 고려하고 있습니다. 피자 헛 다양한 종류의 피자와 시원한 음료를 맛볼 수 있는 곳이에요.

피자 다양한 유형(예: 치즈 피자, 양파 피자, 마살라 피자 등)의 채식 피자 또는 비채식 피자일 수 있으며 소형, 중형, 대형, 특대형의 4가지 크기가 있습니다.

무엇이 PC를 빠르게 만드는가?

차가운 음료 여러 유형(예: Pepsi, Coke, Dew, Sprite, Fanta, Maaza, Limca, Thums-up 등)이 있을 수 있으며 소형, 중형, 대형 3가지 크기가 있습니다.

빌더 패턴의 실제 예

Builder 디자인 패턴의 실제 사례를 단계별로 살펴보겠습니다.

1 단계: 피자와 차가운 음료를 나타내는 인터페이스 항목을 만듭니다.

파일: Item.java
 public interface Item { public String name(); public String size(); public float price(); }// End of the interface Item. 

2 단계: Item 인터페이스를 구현할 추상 클래스 Pizza를 만듭니다.

자바의 배열 길이
파일: Pizza.java
 public abstract class Pizza implements Item{ @Override public abstract float price(); } 

3단계: Item 인터페이스를 구현할 추상 클래스 ColdDrink를 만듭니다.

파일: ColdDrink.java
 public abstract class ColdDrink implements Item{ @Override public abstract float price(); } 

4단계: 추상 클래스 Pizza로 확장될 추상 클래스 VegPizza를 만듭니다.

파일: VegPizza.java
 public abstract class VegPizza extends Pizza{ @Override public abstract float price(); @Override public abstract String name(); @Override public abstract String size(); }// End of the abstract class VegPizza. 

5단계: 추상 클래스 Pizza로 확장될 추상 클래스 NonVegPizza를 만듭니다.

파일: NonVegPizza.java
 public abstract class NonVegPizza extends Pizza{ @Override public abstract float price(); @Override public abstract String name(); @Override public abstract String size(); }// End of the abstract class NonVegPizza. 

6단계: 이제 추상 클래스 VegPizza로 확장될 구체적인 하위 클래스 SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza를 만듭니다.

파일: SmallCheezePizza.java
 public class SmallCheezePizza extends VegPizza{ @Override public float price() { return 170.0f; } @Override public String name() { return 'Cheeze Pizza'; } @Override public String size() { return 'Small size'; } }// End of the SmallCheezePizza class. 
파일: MediumCheezePizza.java
 public class MediumCheezePizza extends VegPizza{ @Override public float price() { return 220.f; } @Override public String name() { return &apos;Cheeze Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } }// End of the MediumCheezePizza class. File: LargeCheezePizza.java <pre> public class LargeCheezePizza extends VegPizza{ @Override public float price() { return 260.0f; } @Override public String name() { return &apos;Cheeze Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } }// End of the LargeCheezePizza class. </pre> File: ExtraLargeCheezePizza.java <pre> public class ExtraLargeCheezePizza extends VegPizza{ @Override public float price() { return 300.f; } @Override public String name() { return &apos;Cheeze Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeCheezePizza class. </pre> <p>Step 7: <b>Now, similarly create concrete sub-classes SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza that will extend to the abstract class VegPizza.</b> </p> File: SmallOnionPizza.java <pre> public class SmallOnionPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Small Size&apos;; } }// End of the SmallOnionPizza class. </pre> File: MediumOnionPizza.java <pre> public class MediumOnionPizza extends VegPizza { @Override public float price() { return 150.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } }// End of the MediumOnionPizza class. </pre> File: LargeOnionPizza.java <pre> public class LargeOnionPizza extends VegPizza{ @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Large size&apos;; } }// End of the LargeOnionPizza class. </pre> File: ExtraLargeOnionPizza.java <pre> public class ExtraLargeOnionPizza extends VegPizza { @Override public float price() { return 200.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeOnionPizza class </pre> <p>Step 8: <b>Now, similarly create concrete sub-classes SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza that will extend to the abstract class VegPizza.</b> </p> File: SmallMasalaPizza.java <pre> public class SmallMasalaPizza extends VegPizza{ @Override public float price() { return 100.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Samll Size&apos;; } }// End of the SmallMasalaPizza class </pre> File: MediumMasalaPizza.java <pre> public class MediumMasalaPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } </pre> File: LargeMasalaPizza.java <pre> public class LargeMasalaPizza extends VegPizza{ @Override public float price() { return 150.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } } //End of the LargeMasalaPizza class </pre> File: ExtraLargeMasalaPizza.java <pre> public class ExtraLargeMasalaPizza extends VegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeMasalaPizza class </pre> <p>Step 9: <b>Now, create concrete sub-classes SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza that will extend to the abstract class NonVegPizza.</b> </p> File: SmallNonVegPizza.java <pre> public class SmallNonVegPizza extends NonVegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Samll Size&apos;; } }// End of the SmallNonVegPizza class </pre> File: MediumNonVegPizza.java <pre> public class MediumNonVegPizza extends NonVegPizza{ @Override public float price() { return 200.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } </pre> File: LargeNonVegPizza.java <pre> public class LargeNonVegPizza extends NonVegPizza{ @Override public float price() { return 220.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } }// End of the LargeNonVegPizza class </pre> File: ExtraLargeNonVegPizza.java <pre> public class ExtraLargeNonVegPizza extends NonVegPizza { @Override public float price() { return 250.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } } // End of the ExtraLargeNonVegPizza class </pre> <p>Step 10: <b>Now, create two abstract classes Pepsi and Coke that will extend abstract class ColdDrink. </b> </p> File: Pepsi.java <pre> public abstract class Pepsi extends ColdDrink { @Override public abstract String name(); @Override public abstract String size(); @Override public abstract float price(); }// End of the Pepsi class </pre> File: Coke.java <pre> public abstract class Coke extends ColdDrink { @Override public abstract String name(); @Override public abstract String size(); @Override public abstract float price(); }// End of the Coke class <p>Step 11: <b>Now, create concrete sub-classes SmallPepsi, MediumPepsi, LargePepsi that will extend to the abstract class Pepsi.</b> </p> File: SmallPepsi.java <pre> public class SmallPepsi extends Pepsi{ @Override public String name() { return &apos;300 ml Pepsi&apos;; } @Override public float price() { return 25.0f; } @Override public String size() { return &apos;Small Size&apos;; } }// End of the SmallPepsi class </pre> File: MediumPepsi.java <pre> public class MediumPepsi extends Pepsi { @Override public String name() { return &apos;500 ml Pepsi&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi class </pre> File: LargePepsi.java <pre> public class LargePepsi extends Pepsi{ @Override public String name() { return &apos;750 ml Pepsi&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class </pre> <p>Step 12: <b>Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke.</b> </p> File: SmallCoke.java <pre> public class SmallCoke extends Coke{ @Override public String name() { return &apos;300 ml Coke&apos;; } @Override public String size() { return &apos;Small Size&apos;; } @Override public float price() { return 25.0f; } }// End of the SmallCoke class </pre> File: MediumCoke.java <pre> public class MediumCoke extends Coke{ @Override public String name() { return &apos;500 ml Coke&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @Override public float price() { return 35.0f; } }// End of the MediumCoke class </pre> File: LargeCoke.java <pre> public class LargeCoke extends Coke { @Override public String name() { return &apos;750 ml Coke&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.println(&apos;Item is:&apos; +item.name()); System.out.println(&apos;Size is:&apos; +item.size()); System.out.println(&apos;Price is: &apos; +item.price()); } } }// End of the OrderedItems class </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(&apos; Enter the choice of Pizza &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Veg-Pizza &apos;); System.out.println(&apos; 2. Non-Veg Pizza &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println(&apos;You ordered Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos; Enter the types of Veg-Pizza &apos;); System.out.println(&apos;------------------------------&apos;); System.out.println(&apos; 1.Cheeze Pizza &apos;); System.out.println(&apos; 2.Onion Pizza &apos;); System.out.println(&apos; 3.Masala Pizza &apos;); System.out.println(&apos; 4.Exit &apos;); System.out.println(&apos;------------------------------&apos;); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println(&apos;You ordered Cheeze Pizza&apos;); System.out.println(&apos;Enter the cheeze pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Cheeze Pizza &apos;); System.out.println(&apos; 2. Medium Cheeze Pizza &apos;); System.out.println(&apos; 3. Large Cheeze Pizza &apos;); System.out.println(&apos; 4. Extra-Large Cheeze Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println(&apos;You ordered Onion Pizza&apos;); System.out.println(&apos;Enter the Onion pizza size&apos;); System.out.println(&apos;----------------------------------&apos;); System.out.println(&apos; 1. Small Onion Pizza &apos;); System.out.println(&apos; 2. Medium Onion Pizza &apos;); System.out.println(&apos; 3. Large Onion Pizza &apos;); System.out.println(&apos; 4. Extra-Large Onion Pizza &apos;); System.out.println(&apos;----------------------------------&apos;); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println(&apos;You ordered Masala Pizza&apos;); System.out.println(&apos;Enter the Masala pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Masala Pizza &apos;); System.out.println(&apos; 2. Medium Masala Pizza &apos;); System.out.println(&apos; 3. Large Masala Pizza &apos;); System.out.println(&apos; 4. Extra-Large Masala Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println(&apos;You ordered Non-Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos;Enter the Non-Veg pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Non-Veg Pizza &apos;); System.out.println(&apos; 2. Medium Non-Veg Pizza &apos;); System.out.println(&apos; 3. Large Non-Veg Pizza &apos;); System.out.println(&apos; 4. Extra-Large Non-Veg Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(&apos; Enter the choice of ColdDrink &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Pepsi &apos;); System.out.println(&apos; 2. Coke &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println(&apos;You ordered Pepsi &apos;); System.out.println(&apos;Enter the Pepsi Size &apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Pepsi &apos;); System.out.println(&apos; 2. Medium Pepsi &apos;); System.out.println(&apos; 3. Large Pepsi &apos;); System.out.println(&apos;------------------------&apos;); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println(&apos;You ordered Coke&apos;); System.out.println(&apos;Enter the Coke Size&apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Coke &apos;); System.out.println(&apos; 2. Medium Coke &apos;); System.out.println(&apos; 3. Large Coke &apos;); System.out.println(&apos; 4. Extra-Large Coke &apos;); System.out.println(&apos;------------------------&apos;); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(&apos;
&apos;); System.out.println(&apos;Total Cost : &apos;+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>  </pre></pre>
파일: ExtraLargeCheezePizza.java
 public class ExtraLargeCheezePizza extends VegPizza{ @Override public float price() { return 300.f; } @Override public String name() { return &apos;Cheeze Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeCheezePizza class. 

7단계: 이제 유사하게 추상 클래스 VegPizza로 확장될 구체적인 하위 클래스 SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza를 만듭니다.

파일: SmallOnionPizza.java
 public class SmallOnionPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Small Size&apos;; } }// End of the SmallOnionPizza class. 
파일: MediumOnionPizza.java
 public class MediumOnionPizza extends VegPizza { @Override public float price() { return 150.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } }// End of the MediumOnionPizza class. 
파일: LargeOnionPizza.java
 public class LargeOnionPizza extends VegPizza{ @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Large size&apos;; } }// End of the LargeOnionPizza class. 
파일: ExtraLargeOnionPizza.java
 public class ExtraLargeOnionPizza extends VegPizza { @Override public float price() { return 200.0f; } @Override public String name() { return &apos;Onion Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeOnionPizza class 

8단계: 이제 유사하게 추상 클래스 VegPizza로 확장될 구체적인 하위 클래스 SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza를 만듭니다.

파일: SmallMasalaPizza.java
 public class SmallMasalaPizza extends VegPizza{ @Override public float price() { return 100.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Samll Size&apos;; } }// End of the SmallMasalaPizza class 
파일: MediumMasalaPizza.java
 public class MediumMasalaPizza extends VegPizza { @Override public float price() { return 120.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } 
파일: LargeMasalaPizza.java
 public class LargeMasalaPizza extends VegPizza{ @Override public float price() { return 150.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } } //End of the LargeMasalaPizza class 
파일: ExtraLargeMasalaPizza.java
 public class ExtraLargeMasalaPizza extends VegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Masala Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } }// End of the ExtraLargeMasalaPizza class 

9단계: 이제 추상 클래스 NonVegPizza로 확장될 구체적인 하위 클래스 SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza를 만듭니다.

파일: SmallNonVegPizza.java
 public class SmallNonVegPizza extends NonVegPizza { @Override public float price() { return 180.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Samll Size&apos;; } }// End of the SmallNonVegPizza class 
파일: MediumNonVegPizza.java
 public class MediumNonVegPizza extends NonVegPizza{ @Override public float price() { return 200.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } 
파일: LargeNonVegPizza.java
 public class LargeNonVegPizza extends NonVegPizza{ @Override public float price() { return 220.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Large Size&apos;; } }// End of the LargeNonVegPizza class 
파일: ExtraLargeNonVegPizza.java
 public class ExtraLargeNonVegPizza extends NonVegPizza { @Override public float price() { return 250.0f; } @Override public String name() { return &apos;Non-Veg Pizza&apos;; } @Override public String size() { return &apos;Extra-Large Size&apos;; } } // End of the ExtraLargeNonVegPizza class 

10단계: 이제 추상 클래스 ColdDrink를 확장할 두 개의 추상 클래스 Pepsi와 Coke를 만듭니다.

파일: Pepsi.java
 public abstract class Pepsi extends ColdDrink { @Override public abstract String name(); @Override public abstract String size(); @Override public abstract float price(); }// End of the Pepsi class 
파일: Coke.java
 public abstract class Coke extends ColdDrink { @Override public abstract String name(); @Override public abstract String size(); @Override public abstract float price(); }// End of the Coke class <p>Step 11: <b>Now, create concrete sub-classes SmallPepsi, MediumPepsi, LargePepsi that will extend to the abstract class Pepsi.</b> </p> File: SmallPepsi.java <pre> public class SmallPepsi extends Pepsi{ @Override public String name() { return &apos;300 ml Pepsi&apos;; } @Override public float price() { return 25.0f; } @Override public String size() { return &apos;Small Size&apos;; } }// End of the SmallPepsi class </pre> File: MediumPepsi.java <pre> public class MediumPepsi extends Pepsi { @Override public String name() { return &apos;500 ml Pepsi&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi class </pre> File: LargePepsi.java <pre> public class LargePepsi extends Pepsi{ @Override public String name() { return &apos;750 ml Pepsi&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class </pre> <p>Step 12: <b>Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke.</b> </p> File: SmallCoke.java <pre> public class SmallCoke extends Coke{ @Override public String name() { return &apos;300 ml Coke&apos;; } @Override public String size() { return &apos;Small Size&apos;; } @Override public float price() { return 25.0f; } }// End of the SmallCoke class </pre> File: MediumCoke.java <pre> public class MediumCoke extends Coke{ @Override public String name() { return &apos;500 ml Coke&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @Override public float price() { return 35.0f; } }// End of the MediumCoke class </pre> File: LargeCoke.java <pre> public class LargeCoke extends Coke { @Override public String name() { return &apos;750 ml Coke&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.println(&apos;Item is:&apos; +item.name()); System.out.println(&apos;Size is:&apos; +item.size()); System.out.println(&apos;Price is: &apos; +item.price()); } } }// End of the OrderedItems class </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(&apos; Enter the choice of Pizza &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Veg-Pizza &apos;); System.out.println(&apos; 2. Non-Veg Pizza &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println(&apos;You ordered Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos; Enter the types of Veg-Pizza &apos;); System.out.println(&apos;------------------------------&apos;); System.out.println(&apos; 1.Cheeze Pizza &apos;); System.out.println(&apos; 2.Onion Pizza &apos;); System.out.println(&apos; 3.Masala Pizza &apos;); System.out.println(&apos; 4.Exit &apos;); System.out.println(&apos;------------------------------&apos;); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println(&apos;You ordered Cheeze Pizza&apos;); System.out.println(&apos;Enter the cheeze pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Cheeze Pizza &apos;); System.out.println(&apos; 2. Medium Cheeze Pizza &apos;); System.out.println(&apos; 3. Large Cheeze Pizza &apos;); System.out.println(&apos; 4. Extra-Large Cheeze Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println(&apos;You ordered Onion Pizza&apos;); System.out.println(&apos;Enter the Onion pizza size&apos;); System.out.println(&apos;----------------------------------&apos;); System.out.println(&apos; 1. Small Onion Pizza &apos;); System.out.println(&apos; 2. Medium Onion Pizza &apos;); System.out.println(&apos; 3. Large Onion Pizza &apos;); System.out.println(&apos; 4. Extra-Large Onion Pizza &apos;); System.out.println(&apos;----------------------------------&apos;); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println(&apos;You ordered Masala Pizza&apos;); System.out.println(&apos;Enter the Masala pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Masala Pizza &apos;); System.out.println(&apos; 2. Medium Masala Pizza &apos;); System.out.println(&apos; 3. Large Masala Pizza &apos;); System.out.println(&apos; 4. Extra-Large Masala Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println(&apos;You ordered Non-Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos;Enter the Non-Veg pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Non-Veg Pizza &apos;); System.out.println(&apos; 2. Medium Non-Veg Pizza &apos;); System.out.println(&apos; 3. Large Non-Veg Pizza &apos;); System.out.println(&apos; 4. Extra-Large Non-Veg Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(&apos; Enter the choice of ColdDrink &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Pepsi &apos;); System.out.println(&apos; 2. Coke &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println(&apos;You ordered Pepsi &apos;); System.out.println(&apos;Enter the Pepsi Size &apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Pepsi &apos;); System.out.println(&apos; 2. Medium Pepsi &apos;); System.out.println(&apos; 3. Large Pepsi &apos;); System.out.println(&apos;------------------------&apos;); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println(&apos;You ordered Coke&apos;); System.out.println(&apos;Enter the Coke Size&apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Coke &apos;); System.out.println(&apos; 2. Medium Coke &apos;); System.out.println(&apos; 3. Large Coke &apos;); System.out.println(&apos; 4. Extra-Large Coke &apos;); System.out.println(&apos;------------------------&apos;); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(&apos;
&apos;); System.out.println(&apos;Total Cost : &apos;+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>  </pre>
파일: MediumPepsi.java
 public class MediumPepsi extends Pepsi { @Override public String name() { return &apos;500 ml Pepsi&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @Override public float price() { return 35.0f; } }// End of the MediumPepsi class 
파일: LargePepsi.java
 public class LargePepsi extends Pepsi{ @Override public String name() { return &apos;750 ml Pepsi&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @Override public float price() { return 50.0f; } }// End of the LargePepsi class 

12단계: 이제 추상 클래스 Coke로 확장될 구체적인 하위 클래스 SmallCoke, MediumCoke, LargeCoke를 만듭니다.

foreach 타이프스크립트
파일: SmallCoke.java
 public class SmallCoke extends Coke{ @Override public String name() { return &apos;300 ml Coke&apos;; } @Override public String size() { return &apos;Small Size&apos;; } @Override public float price() { return 25.0f; } }// End of the SmallCoke class 
파일: MediumCoke.java
 public class MediumCoke extends Coke{ @Override public String name() { return &apos;500 ml Coke&apos;; } @Override public String size() { return &apos;Medium Size&apos;; } @Override public float price() { return 35.0f; } }// End of the MediumCoke class 
파일: LargeCoke.java
 public class LargeCoke extends Coke { @Override public String name() { return &apos;750 ml Coke&apos;; } @Override public String size() { return &apos;Large Size&apos;; } @Override public float price() { return 50.0f; } }// End of the LargeCoke class <p>Step 13: <b>Create an OrderedItems class that are having Item objects defined above.</b> </p> File: OrderedItems.java <pre> import java.util.ArrayList; import java.util.List; public class OrderedItems { List items=new ArrayList(); public void addItems(Item item){ items.add(item); } public float getCost(){ float cost=0.0f; for (Item item : items) { cost+=item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.println(&apos;Item is:&apos; +item.name()); System.out.println(&apos;Size is:&apos; +item.size()); System.out.println(&apos;Price is: &apos; +item.price()); } } }// End of the OrderedItems class </pre> <p>Step 14: <b>Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.</b> </p> File: OrdereBuilder.java <pre> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(&apos; Enter the choice of Pizza &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Veg-Pizza &apos;); System.out.println(&apos; 2. Non-Veg Pizza &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println(&apos;You ordered Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos; Enter the types of Veg-Pizza &apos;); System.out.println(&apos;------------------------------&apos;); System.out.println(&apos; 1.Cheeze Pizza &apos;); System.out.println(&apos; 2.Onion Pizza &apos;); System.out.println(&apos; 3.Masala Pizza &apos;); System.out.println(&apos; 4.Exit &apos;); System.out.println(&apos;------------------------------&apos;); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println(&apos;You ordered Cheeze Pizza&apos;); System.out.println(&apos;Enter the cheeze pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Cheeze Pizza &apos;); System.out.println(&apos; 2. Medium Cheeze Pizza &apos;); System.out.println(&apos; 3. Large Cheeze Pizza &apos;); System.out.println(&apos; 4. Extra-Large Cheeze Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println(&apos;You ordered Onion Pizza&apos;); System.out.println(&apos;Enter the Onion pizza size&apos;); System.out.println(&apos;----------------------------------&apos;); System.out.println(&apos; 1. Small Onion Pizza &apos;); System.out.println(&apos; 2. Medium Onion Pizza &apos;); System.out.println(&apos; 3. Large Onion Pizza &apos;); System.out.println(&apos; 4. Extra-Large Onion Pizza &apos;); System.out.println(&apos;----------------------------------&apos;); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println(&apos;You ordered Masala Pizza&apos;); System.out.println(&apos;Enter the Masala pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Masala Pizza &apos;); System.out.println(&apos; 2. Medium Masala Pizza &apos;); System.out.println(&apos; 3. Large Masala Pizza &apos;); System.out.println(&apos; 4. Extra-Large Masala Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println(&apos;You ordered Non-Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos;Enter the Non-Veg pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Non-Veg Pizza &apos;); System.out.println(&apos; 2. Medium Non-Veg Pizza &apos;); System.out.println(&apos; 3. Large Non-Veg Pizza &apos;); System.out.println(&apos; 4. Extra-Large Non-Veg Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(&apos; Enter the choice of ColdDrink &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Pepsi &apos;); System.out.println(&apos; 2. Coke &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println(&apos;You ordered Pepsi &apos;); System.out.println(&apos;Enter the Pepsi Size &apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Pepsi &apos;); System.out.println(&apos; 2. Medium Pepsi &apos;); System.out.println(&apos; 3. Large Pepsi &apos;); System.out.println(&apos;------------------------&apos;); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println(&apos;You ordered Coke&apos;); System.out.println(&apos;Enter the Coke Size&apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Coke &apos;); System.out.println(&apos; 2. Medium Coke &apos;); System.out.println(&apos; 3. Large Coke &apos;); System.out.println(&apos; 4. Extra-Large Coke &apos;); System.out.println(&apos;------------------------&apos;); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method </pre> <p>Step 15: <b>Create a BuilderDemo class that will use the OrderBuilder class.</b> </p> File: Prototype.java <pre> import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(&apos;
&apos;); System.out.println(&apos;Total Cost : &apos;+ orderedItems.getCost()); } }// End of the BuilderDemo class </pre> <hr> download this Builder Pattern Example <h4>Output</h4>  

14단계: OrderedItems 클래스의 객체 생성을 담당할 OrderBuilder 클래스를 생성합니다.

파일: OrdereBuilder.java
 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderBuilder { public OrderedItems preparePizza() throws IOException{ OrderedItems itemsOrder=new OrderedItems(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println(&apos; Enter the choice of Pizza &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Veg-Pizza &apos;); System.out.println(&apos; 2. Non-Veg Pizza &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine()); switch(pizzaandcolddrinkchoice) { case 1:{ System.out.println(&apos;You ordered Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos; Enter the types of Veg-Pizza &apos;); System.out.println(&apos;------------------------------&apos;); System.out.println(&apos; 1.Cheeze Pizza &apos;); System.out.println(&apos; 2.Onion Pizza &apos;); System.out.println(&apos; 3.Masala Pizza &apos;); System.out.println(&apos; 4.Exit &apos;); System.out.println(&apos;------------------------------&apos;); int vegpizzachoice=Integer.parseInt(br.readLine()); switch(vegpizzachoice) { case 1: { System.out.println(&apos;You ordered Cheeze Pizza&apos;); System.out.println(&apos;Enter the cheeze pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Cheeze Pizza &apos;); System.out.println(&apos; 2. Medium Cheeze Pizza &apos;); System.out.println(&apos; 3. Large Cheeze Pizza &apos;); System.out.println(&apos; 4. Extra-Large Cheeze Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int cheezepizzasize=Integer.parseInt(br.readLine()); switch(cheezepizzasize) { case 1: itemsOrder.addItems(new SmallCheezePizza()); break; case 2: itemsOrder.addItems(new MediumCheezePizza()); break; case 3: itemsOrder.addItems(new LargeCheezePizza()); break; case 4: itemsOrder.addItems(new ExtraLargeCheezePizza()); break; case 2: { System.out.println(&apos;You ordered Onion Pizza&apos;); System.out.println(&apos;Enter the Onion pizza size&apos;); System.out.println(&apos;----------------------------------&apos;); System.out.println(&apos; 1. Small Onion Pizza &apos;); System.out.println(&apos; 2. Medium Onion Pizza &apos;); System.out.println(&apos; 3. Large Onion Pizza &apos;); System.out.println(&apos; 4. Extra-Large Onion Pizza &apos;); System.out.println(&apos;----------------------------------&apos;); int onionpizzasize=Integer.parseInt(br.readLine()); switch(onionpizzasize) { case 1: itemsOrder.addItems(new SmallOnionPizza()); break; case 2: itemsOrder.addItems(new MediumOnionPizza()); break; case 3: itemsOrder.addItems(new LargeOnionPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeOnionPizza()); break; } } break; case 3: { System.out.println(&apos;You ordered Masala Pizza&apos;); System.out.println(&apos;Enter the Masala pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Masala Pizza &apos;); System.out.println(&apos; 2. Medium Masala Pizza &apos;); System.out.println(&apos; 3. Large Masala Pizza &apos;); System.out.println(&apos; 4. Extra-Large Masala Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int masalapizzasize=Integer.parseInt(br.readLine()); switch(masalapizzasize) { case 1: itemsOrder.addItems(new SmallMasalaPizza()); break; case 2: itemsOrder.addItems(new MediumMasalaPizza()); break; case 3: itemsOrder.addItems(new LargeMasalaPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeMasalaPizza()); break; } } break; } } break;// Veg- pizza choice completed. case 2: { System.out.println(&apos;You ordered Non-Veg Pizza&apos;); System.out.println(&apos;

&apos;); System.out.println(&apos;Enter the Non-Veg pizza size&apos;); System.out.println(&apos;------------------------------------&apos;); System.out.println(&apos; 1. Small Non-Veg Pizza &apos;); System.out.println(&apos; 2. Medium Non-Veg Pizza &apos;); System.out.println(&apos; 3. Large Non-Veg Pizza &apos;); System.out.println(&apos; 4. Extra-Large Non-Veg Pizza &apos;); System.out.println(&apos;------------------------------------&apos;); int nonvegpizzasize=Integer.parseInt(br.readLine()); switch(nonvegpizzasize) { case 1: itemsOrder.addItems(new SmallNonVegPizza()); break; case 2: itemsOrder.addItems(new MediumNonVegPizza()); break; case 3: itemsOrder.addItems(new LargeNonVegPizza()); break; case 4: itemsOrder.addItems(new ExtraLargeNonVegPizza()); break; } } break; default: { break; } }//end of main Switch //continued?.. System.out.println(&apos; Enter the choice of ColdDrink &apos;); System.out.println(&apos;============================&apos;); System.out.println(&apos; 1. Pepsi &apos;); System.out.println(&apos; 2. Coke &apos;); System.out.println(&apos; 3. Exit &apos;); System.out.println(&apos;============================&apos;); int coldDrink=Integer.parseInt(br.readLine()); switch (coldDrink) { case 1: { System.out.println(&apos;You ordered Pepsi &apos;); System.out.println(&apos;Enter the Pepsi Size &apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Pepsi &apos;); System.out.println(&apos; 2. Medium Pepsi &apos;); System.out.println(&apos; 3. Large Pepsi &apos;); System.out.println(&apos;------------------------&apos;); int pepsisize=Integer.parseInt(br.readLine()); switch(pepsisize) { case 1: itemsOrder.addItems(new SmallPepsi()); break; case 2: itemsOrder.addItems(new MediumPepsi()); break; case 3: itemsOrder.addItems(new LargePepsi()); break; } } break; case 2: { System.out.println(&apos;You ordered Coke&apos;); System.out.println(&apos;Enter the Coke Size&apos;); System.out.println(&apos;------------------------&apos;); System.out.println(&apos; 1. Small Coke &apos;); System.out.println(&apos; 2. Medium Coke &apos;); System.out.println(&apos; 3. Large Coke &apos;); System.out.println(&apos; 4. Extra-Large Coke &apos;); System.out.println(&apos;------------------------&apos;); int cokesize=Integer.parseInt(br.readLine()); switch(cokesize) { case 1: itemsOrder.addItems(new SmallCoke()); break; case 2: itemsOrder.addItems(new MediumCoke()); break; case 3: itemsOrder.addItems(new LargeCoke()); break; } } break; default: { break; } }//End of the Cold-Drink switch return itemsOrder; } //End of the preparePizza() method 

15단계: OrderBuilder 클래스를 사용할 BuilderDemo 클래스를 만듭니다.

파일: Prototype.java
 import java.io.IOException; public class BuilderDemo { public static void main(String[] args) throws IOException { // TODO code application logic here OrderBuilder builder=new OrderBuilder(); OrderedItems orderedItems=builder.preparePizza(); orderedItems.showItems(); System.out.println(&apos;
&apos;); System.out.println(&apos;Total Cost : &apos;+ orderedItems.getCost()); } }// End of the BuilderDemo class 

이 빌더 패턴 예제를 다운로드하세요

산출