logo

Padrão de Design do Construtor

  1. Padrão de Design do Construtor
  2. Vantagem do Builder DP
  3. Uso do Construtor DP
  4. UML do Construtor DP
  5. Exemplo de Construtor DP

Padrão do Construtor diz que 'construir um objeto complexo a partir de objetos simples usando uma abordagem passo a passo'

É usado principalmente quando o objeto não pode ser criado em uma única etapa, como na desserialização de um objeto complexo.

Vantagem do padrão de design do construtor

As principais vantagens do Builder Pattern são as seguintes:

  • Ele fornece uma separação clara entre a construção e a representação de um objeto.
  • Proporciona melhor controle sobre o processo de construção.
  • Suporta alterar a representação interna de objetos.

Exemplo de padrão UML para construtor


Exemplo de padrão de design do construtor

Para criar um exemplo simples de padrão de design de construtor, você precisa seguir as 6 etapas a seguir.

  1. Criar interface de embalagem
  2. Crie 2 classes abstratas CD e Empresa
  3. Crie 2 classes de implementação de Empresa: Sony e Samsung
  4. Crie a classe CDType
  5. Crie a classe CDBuilder
  6. Crie a classe BuilderDemo

1) Criar interface de embalagem

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

2) Crie 2 classes abstratas CD e Empresa

Crie um CD de classe abstrata que implementará a interface Packing.

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

3) Crie 2 classes de implementação de Empresa: Sony e Samsung

Arquivo: 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. 
Arquivo: 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) Crie a classe CDType

Arquivo: 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) Crie a classe CDBuilder

Arquivo: 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) Crie a classe BuilderDemo

Arquivo: 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(); } } 

baixe este exemplo de padrão de construtor

Saída do exemplo acima

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

Outro exemplo do mundo real de Builder Pattern

UML para padrão Builder:

Estamos considerando um caso de negócios de pizzaria onde podemos conseguir diferentes variedades de pizza e bebidas geladas.

pizza pode ser uma pizza vegetariana ou não vegetariana de vários tipos (como pizza de queijo, pizza de cebola, pizza masala, etc.) e terá 4 tamanhos, ou seja, pequeno, médio, grande, extragrande.

Bebida gelada podem ser de vários tipos (como Pepsi, Coca, Dew, Sprite, Fanta, Maaza, Limca, Thums-up etc.) e terão 3 tamanhos pequeno, médio, grande.

Exemplo do mundo real de padrão de construtor

Vamos ver o exemplo passo a passo do mundo real do Builder Design Pattern.

Passo 1: Crie um Item de interface que represente a Pizza e o Refrigerante.

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

Passo 2: Crie uma classe abstrata Pizza que será implementada na interface Item.

Arquivo: Pizza.java
 public abstract class Pizza implements Item{ @Override public abstract float price(); } 

Etapa 3: Crie uma classe abstrata ColdDrink que será implementada na interface Item.

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

Passo 4: Crie uma classe abstrata VegPizza que se estenderá à classe abstrata Pizza.

Arquivo: 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. 

Etapa 5: Crie uma classe abstrata NonVegPizza que se estenderá à classe abstrata Pizza.

Arquivo: 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. 

Etapa 6: Agora, crie subclasses concretas SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza que se estenderão à classe abstrata VegPizza.

Alya Manasa
Arquivo: 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. 
Arquivo: 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>
Arquivo: 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. 

Etapa 7: Agora, crie da mesma forma subclasses concretas SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza que se estenderão à classe abstrata VegPizza.

Arquivo: 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. 
Arquivo: 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. 
Arquivo: 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. 
Arquivo: 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 

Etapa 8: Agora, crie de forma semelhante subclasses concretas SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza que se estenderão à classe abstrata VegPizza.

Arquivo: 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 
Arquivo: 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;; } 
Arquivo: 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 
Arquivo: 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 

Etapa 9: Agora, crie subclasses concretas SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza que se estenderão à classe abstrata NonVegPizza.

Arquivo: 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 
Arquivo: 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;; } 
Arquivo: 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 
Arquivo: 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 

Etapa 10: Agora, crie duas classes abstratas Pepsi e Coke que estenderão a classe abstrata ColdDrink.

Arquivo: 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 
Arquivo: 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>
Arquivo: 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 
Arquivo: 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 

Etapa 12: Agora, crie subclasses concretas SmallCoke, MediumCoke, LargeCoke que se estenderão à classe abstrata Coke.

Arquivo: 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 
Arquivo: 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 
Arquivo: 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>  

Etapa 14: Crie uma classe OrderBuilder que será responsável por criar os objetos da classe OrderedItems.

Arquivo: 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 

Etapa 15: Crie uma classe BuilderDemo que usará a classe OrderBuilder.

Arquivo: Protótipo.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 

baixe este exemplo de padrão do construtor

Saída