Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Builder Pattern


Builder Pattern says that “construct a complex object from simple objects using step-by-step approach”

It is mostly used when object can’t be created in single step like in the de-serialization of a complex object.

Advantage of Builder Design Pattern

The main advantages of Builder Pattern are as follows:

  • It provides clear separation between the construction and representation of an object.
  • It provides better control over construction process.
  • It supports to change the internal representation of objects.

UML for Builder Pattern Example

builderuml1

Example of Builder Design Pattern

To create simple example of builder design pattern, you need to follow 6 following steps.

  1. Create Packing interface
  2. Create 2 abstract classes CD and Company
  3. Create 2 implementation classes of Company: Sony and Samsung
  4. Create the CDType class
  5. Create the CDBuilder class
  6. Create the BuilderDemo class

1) Create Packing interface

File: Packing.java
  1. public interface Packing {
  2.             public String pack();
  3.             public int price();
  4. }

2) Create 2 abstract classes CD and Company

Create an abstract class CD which will implement Packing interface.

File: CD.java
  1. public abstract class CD implements Packing{
  2. public abstract String pack();
  3. }
File: Company.java
  1. public abstract class Company extends CD{
  2.    public abstract int price();
  3. }

3) Create 2 implementation classes of Company: Sony and Samsung

File: Sony.java
  1. public class Sony extends Company{
  2.     @Override
  3.         public int price(){
  4.                         return 20;
  5.       }
  6.     @Override
  7.     public String pack(){
  8.              return “Sony CD”;
  9.         }
  10. }//End of the Sony class.
File: Samsung.java
  1. public class Samsung extends Company {
  2.     @Override
  3.         public int price(){
  4.                         return 15;
  5.     }
  6.     @Override
  7.     public String pack(){
  8.              return “Samsung CD”;
  9.         }
  10. }//End of the Samsung class.

4) Create the CDType class

File: CDType.java
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class CDType {
  4.              private List<Packing> items=new ArrayList<Packing>();
  5.              public void addItem(Packing packs) {
  6.                     items.add(packs);
  7.              }
  8.              public void getCost(){
  9.               for (Packing packs : items) {
  10.                         packs.price();
  11.               }
  12.              }
  13.              public void showItems(){
  14.               for (Packing packing : items){
  15.              System.out.print(“CD name : “+packing.pack());
  16.              System.out.println(“, Price : “+packing.price());
  17.           }
  18.             }
  19. }//End of the CDType class.

5) Create the CDBuilder class

File: CDBuilder.java
  1. public class CDBuilder {
  2.                   public CDType buildSonyCD(){
  3.                      CDType cds=new CDType();
  4.                      cds.addItem(new Sony());
  5.                      return cds;
  6.               }
  7.               public CDType buildSamsungCD(){
  8.              CDType cds=new CDType();
  9.              cds.addItem(new Samsung());
  10.              return cds;
  11.               }
  12. }// End of the CDBuilder class.

6) Create the BuilderDemo class

File: BuilderDemo.java
  1. public class BuilderDemo{
  2.  public static void main(String args[]){
  3.    CDBuilder cdBuilder=new CDBuilder();
  4.    CDType cdType1=cdBuilder.buildSonyCD();
  5.    cdType1.showItems();
  6.    CDType cdType2=cdBuilder.buildSamsungCD();
  7.    cdType2.showItems();
  8.  }
  9. }

download this builder pattern example

Output of the above example

  1. CD name : Sony CD, Price : 20
  2. CD name : Samsung CD, Price : 15

Another Real world example of Builder Pattern

UML for Builder Pattern:

We are considering a business case of pizza-hut where we can get different varieties of pizza and cold-drink.

Pizza can be either a Veg pizza or Non-Veg pizza of several types (like cheese pizza, onion pizza, masala-pizza etc) and will be of 4 sizes i.e. small, medium, large, extra-large.

Cold-drink can be of several types (like Pepsi, Coke, Dew, Sprite, Fanta, Maaza, Limca, Thums-up etc.) and will be of 3 sizes small, medium, large.

builderuml2

Real world example of builder pattern

Let’s see the step by step real world example of Builder Design Pattern.

Step 1:Create an interface Item that represents the Pizza and Cold-drink.

File: Item.java
  1. public  interface  Item
  2. {
  3.    public String name();
  4.     public String size();
  5.     public float price();
  6. }// End of the interface Item.

Step 2:Create an abstract class Pizza that will implement to the interface Item.

File: Pizza.java
  1. public abstract class Pizza implements Item{
  2.         @Override
  3.         public abstract float price();
  4. }

Step 3:Create an abstract class ColdDrink that will implement to the interface Item.

File: ColdDrink.java
  1. public abstract class ColdDrink implements Item{
  2.  @Override
  3.  public abstract float price();

Step 4:Create an abstract class VegPizza that will extend to the abstract class Pizza.

File: VegPizza.java
  1. public abstract class VegPizza extends Pizza{
  2.     @Override
  3.     public abstract float price();
  4.     @Override
  5.     public abstract  String name();
  6.     @Override
  7.     public abstract  String size();
  8. }// End of the abstract class VegPizza.

Step 5:Create an abstract class NonVegPizza that will extend to the abstract class Pizza.

File: NonVegPizza.java
  1. public abstract class NonVegPizza extends Pizza{
  2.     @Override
  3.     public abstract float price();
  4.     @Override
  5.     public abstract String name();
  6.     @Override
  7.     public abstract String size();
  8. }// End of the abstract class NonVegPizza.

Step 6:Now, create concrete sub-classes SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza that will extend to the abstract class VegPizza.

File: SmallCheezePizza.java
  1. public class SmallCheezePizza extends VegPizza{
  2.     @Override
  3.     public float price() {
  4.         return 170.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.         return “Cheeze Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.        return “Small size”;
  13.     }
  14. }// End of the SmallCheezePizza class.
File: MediumCheezePizza.java
  1. public class MediumCheezePizza extends VegPizza{
  2.     @Override
  3.     public float price() {
  4.        return  220.f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.         return “Cheeze Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.      return “Medium Size”;
  13.  }
  14. }// End of the MediumCheezePizza class.
  15. </textaera></div>
  16. <div id=“filename”>File: LargeCheezePizza.java</div>
  17. <div class=“codeblock”><textarea  name=“code” class=“java”>
  18. public class LargeCheezePizza extends VegPizza{
  19.     @Override
  20.     public float price() {
  21.         return 260.0f;
  22.     }
  23.     @Override
  24.     public String name() {
  25.         return “Cheeze Pizza”;
  26.     }
  27.     @Override
  28.     public String size() {
  29.         return “Large Size”;
  30.     }
  31. }// End of the LargeCheezePizza class.
File: ExtraLargeCheezePizza.java
  1. public class ExtraLargeCheezePizza extends VegPizza{
  2.     @Override
  3.     public float price() {
  4.         return 300.f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.         return  “Cheeze Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.         return “Extra-Large Size”;
  13.     }
  14. }// End of the ExtraLargeCheezePizza class.

Step 7:Now, similarly create concrete sub-classes SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza that will extend to the abstract class VegPizza.

File: SmallOnionPizza.java
  1. public class SmallOnionPizza extends VegPizza {
  2.     @Override
  3.     public float price() {
  4.         return 120.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.         return  “Onion Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.        return  “Small Size”;
  13.     }
  14. }// End of the SmallOnionPizza class.
File: MediumOnionPizza.java
  1. public class MediumOnionPizza extends VegPizza {
  2.     @Override
  3.     public float price() {
  4.         return 150.0f;
  5.     }
  6.    @Override
  7.     public String name() {
  8.         return  “Onion Pizza”;
  9.     }
  10.    @Override
  11.     public String size() {
  12.        return  “Medium Size”;
  13.     }
  14. }// End of the MediumOnionPizza class.
File: LargeOnionPizza.java
  1. public class LargeOnionPizza extends  VegPizza{
  2.     @Override
  3.     public float price() {
  4.         return 180.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.          return “Onion Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.        return  “Large size”;
  13.     }
  14. }// End of the LargeOnionPizza class.
File: ExtraLargeOnionPizza.java
  1. public class ExtraLargeOnionPizza extends VegPizza {
  2.     @Override
  3.     public float price() {
  4.         return 200.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.          return  “Onion Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.        return  “Extra-Large Size”;
  13.     }
  14. }// End of the ExtraLargeOnionPizza class

Step 8:Now, similarly create concrete sub-classes SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza that will extend to the abstract class VegPizza.

File: SmallMasalaPizza.java
  1. public class SmallMasalaPizza extends VegPizza{
  2.     @Override
  3.     public float price() {
  4.         return 100.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.         return  “Masala Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.        return  “Samll Size”;
  13.     }
  14. }// End of the SmallMasalaPizza class
File: MediumMasalaPizza.java
  1. public class MediumMasalaPizza extends VegPizza {
  2.     @Override
  3.     public float price() {
  4.         return 120.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.         return  “Masala Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.        return  “Medium Size”;
  13.     }
File: LargeMasalaPizza.java
  1. public class LargeMasalaPizza extends  VegPizza{
  2.     @Override
  3.     public float price() {
  4.         return 150.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.         return  “Masala Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.        return  “Large Size”;
  13.     }
  14. //End of the LargeMasalaPizza class
File: ExtraLargeMasalaPizza.java
  1. public class ExtraLargeMasalaPizza extends VegPizza {
  2.     @Override
  3.     public float price() {
  4.         return 180.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.         return  “Masala Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.        return  “Extra-Large Size”;
  13.     }
  14. }// End of the ExtraLargeMasalaPizza class 

Step 9:Now, create concrete sub-classes SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza that will extend to the abstract class NonVegPizza.

File: SmallNonVegPizza.java
  1. public class SmallNonVegPizza extends NonVegPizza {
  2.     @Override
  3.     public float price() {
  4.         return 180.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.        return “Non-Veg Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.         return “Samll Size”;
  13.     }
  14. }// End of the SmallNonVegPizza class
File: MediumNonVegPizza.java
  1. public class MediumNonVegPizza extends NonVegPizza{
  2.     @Override
  3.     public float price() {
  4.         return 200.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.        return “Non-Veg Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.         return “Medium Size”;
  13.     }
File: LargeNonVegPizza.java
  1. public class LargeNonVegPizza extends NonVegPizza{
  2.     @Override
  3.     public float price() {
  4.         return 220.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.        return “Non-Veg Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.         return “Large Size”;
  13.     }
  14. }// End of the LargeNonVegPizza class
File: ExtraLargeNonVegPizza.java
  1. public class ExtraLargeNonVegPizza extends NonVegPizza {
  2.     @Override
  3.     public float price() {
  4.         return 250.0f;
  5.     }
  6.     @Override
  7.     public String name() {
  8.        return “Non-Veg Pizza”;
  9.     }
  10.     @Override
  11.     public String size() {
  12.         return “Extra-Large Size”;
  13.     }
  14. }
  15.  // End of the ExtraLargeNonVegPizza class

Step 10:Now, create two abstract classes Pepsi and Coke that will extend abstract class ColdDrink.

File: Pepsi.java
  1. public abstract class Pepsi extends ColdDrink {
  2.     @Override
  3.     public abstract  String name();
  4.     @Override
  5.     public abstract  String size();
  6.     @Override
  7.     public abstract  float price();
  8. }// End of the Pepsi class
File: Coke.java
  1. public abstract class Coke  extends ColdDrink {
  2.     @Override
  3.     public abstract  String name();
  4.     @Override
  5.     public abstract  String size();
  6.     @Override
  7.     public abstract  float price();
  8. }// End of the Coke class
  9. </textaea></div>
  10. <p>Step 11:<b>Now, create concrete sub-classes SmallPepsi, MediumPepsi, LargePepsi that will extend to the abstract class Pepsi.</b></p>
  11. <div id=“filename”>File: SmallPepsi.java</div>
  12. <div class=“codeblock”><textarea  name=“code” class=“java”>
  13. public class SmallPepsi  extends Pepsi{
  14.     @Override
  15.     public String name() {
  16.        return “300 ml Pepsi”;
  17.     }
  18.     @Override
  19.     public float price() {
  20.         return 25.0f;
  21.     }
  22.     @Override
  23.     public String size() {
  24.         return “Small Size”;
  25.     }
  26. }// End of the SmallPepsi class
File: MediumPepsi.java
  1. public class MediumPepsi extends Pepsi {
  2.     @Override
  3.     public String name() {
  4.        return “500 ml Pepsi”;
  5.     }
  6.     @Override
  7.     public String size() {
  8.         return “Medium Size”;
  9.     }
  10.     @Override
  11.     public float price() {
  12.         return 35.0f;
  13.     }
  14. }// End of the MediumPepsi class
File: LargePepsi.java
  1. public class LargePepsi extends Pepsi{
  2.     @Override
  3.     public String name() {
  4.        return “750 ml Pepsi”;
  5.     }
  6.     @Override
  7.     public String size() {
  8.         return “Large Size”;
  9.     }
  10.     @Override
  11.     public float price() {
  12.         return 50.0f;
  13.     }
  14. }// End of the LargePepsi class

Step 12:Now, create concrete sub-classes SmallCoke, MediumCoke, LargeCoke that will extend to the abstract class Coke.

File: SmallCoke.java
  1. public class SmallCoke extends Coke{
  2.     @Override
  3.     public String name() {
  4.          return “300 ml Coke”;
  5.     }
  6.     @Override
  7.     public String size() {
  8.         return “Small Size”;
  9.     }
  10.     @Override
  11.     public float price() {
  12.         return  25.0f;
  13.     }
  14.  }// End of the SmallCoke class
File: MediumCoke.java
  1. public class MediumCoke extends Coke{
  2.     @Override
  3.     public String name() {
  4.          return “500 ml Coke”;
  5.     }
  6.     @Override
  7.     public String size() {
  8.         return “Medium Size”;
  9.     }
  10.     @Override
  11.     public float price() {
  12.         return  35.0f;
  13.    }
  14. }// End of the MediumCoke class
File: LargeCoke.java
  1. public class LargeCoke extends Coke {
  2.      @Override
  3.     public String name() {
  4.          return “750 ml Coke”;
  5.     }
  6.     @Override
  7.     public String size() {
  8.         return “Large Size”;
  9.     }
  10.     @Override
  11.     public float price() {
  12.         return  50.0f;
  13.     }
  14. }// End of the LargeCoke class
  15. </textrea></div>
  16. <p>Step 13:<b>Create an OrderedItems class that are having Item objects defined above.</b></p>
  17. <div id=“filename”>File: OrderedItems.java</div>
  18. <div class=“codeblock”><textarea  name=“code” class=“java”>
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. public class OrderedItems {
  22.     List<Item> items=new ArrayList<Item>();
  23.     public void addItems(Item item){
  24.         items.add(item);
  25.     }
  26.     public float getCost(){
  27.         float cost=0.0f;
  28.             for (Item item : items) {
  29.                 cost+=item.price();
  30.              }
  31.         return cost;
  32.     }
  33.     public void showItems(){
  34.         for (Item item : items) {
  35.             System.out.println(“Item is:” +item.name());
  36.             System.out.println(“Size is:” +item.size());
  37.             System.out.println(“Price is: “ +item.price());
  38.         }
  39.     }
  40. }// End of the OrderedItems class

Step 14:Create an OrderBuilder class that will be responsible to create the objects of OrderedItems class.

File: OrdereBuilder.java
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class OrderBuilder {
  5.     public OrderedItems preparePizza() throws IOException{
  6.         OrderedItems itemsOrder=new OrderedItems();
  7.          BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
  8.         System.out.println(” Enter the choice of Pizza “);
  9.         System.out.println(“============================”);
  10.         System.out.println(”        1. Veg-Pizza       “);
  11.         System.out.println(”        2. Non-Veg Pizza   “);
  12.         System.out.println(”        3. Exit            “);
  13.         System.out.println(“============================”);
  14.         int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine());
  15.         switch(pizzaandcolddrinkchoice)
  16.         {
  17.            case 1:{
  18.                      System.out.println(“You ordered Veg Pizza”);
  19.                      System.out.println(“\n\n”);
  20.                      System.out.println(” Enter the types of Veg-Pizza “);
  21.                      System.out.println(“——————————“);
  22.                      System.out.println(”        1.Cheeze Pizza        “);
  23.                      System.out.println(”        2.Onion Pizza        “);
  24.                      System.out.println(”        3.Masala Pizza        “);
  25.                      System.out.println(”        4.Exit            “);
  26.                      System.out.println(“——————————“);
  27.                                   int vegpizzachoice=Integer.parseInt(br.readLine());
  28.                      switch(vegpizzachoice)
  29.                      {
  30.                           case 1:
  31.                                 {
  32.                                     System.out.println(“You ordered Cheeze Pizza”);
  33.                                     System.out.println(“Enter the cheeze pizza size”);
  34.                                     System.out.println(“————————————“);
  35.                                     System.out.println(”    1. Small Cheeze Pizza “);
  36.                                     System.out.println(”    2. Medium Cheeze Pizza “);
  37.                                     System.out.println(”    3. Large Cheeze Pizza “);
  38.                                     System.out.println(”    4. Extra-Large Cheeze Pizza “);
  39.                                     System.out.println(“————————————“);
  40.                                     int cheezepizzasize=Integer.parseInt(br.readLine());
  41.                                     switch(cheezepizzasize)
  42.                                         {
  43.                                               case 1:
  44.                                                    itemsOrder.addItems(new SmallCheezePizza());
  45.                                                    break;
  46.                                              case 2:
  47.                                                    itemsOrder.addItems(new MediumCheezePizza());
  48.                                                    break;
  49.                                              case 3:
  50.                                                    itemsOrder.addItems(new LargeCheezePizza());
  51.                                                    break;
  52.                                              case 4:
  53.                                                    itemsOrder.addItems(new ExtraLargeCheezePizza());
  54.                                                    break;
  55.                           case 2:
  56.                                 {
  57.                                     System.out.println(“You ordered Onion Pizza”);
  58.                                     System.out.println(“Enter the Onion pizza size”);
  59.                                     System.out.println(“———————————-“);
  60.                                     System.out.println(”    1. Small Onion Pizza “);
  61.                                     System.out.println(”    2. Medium Onion Pizza “);
  62.                                     System.out.println(”    3. Large Onion Pizza “);
  63.                                     System.out.println(”    4. Extra-Large Onion Pizza “);
  64.                                     System.out.println(“———————————-“);
  65.                                     int onionpizzasize=Integer.parseInt(br.readLine());
  66.                                     switch(onionpizzasize)
  67.                                               {
  68.                                                   case 1:
  69.                                                    itemsOrder.addItems(new SmallOnionPizza());
  70.                                                    break;
  71.                                                   case 2:
  72.                                                    itemsOrder.addItems(new MediumOnionPizza());
  73.                                                    break;
  74.                                                   case 3:
  75.                                                    itemsOrder.addItems(new LargeOnionPizza());
  76.                                                    break;
  77.                                                   case 4:
  78.                                                    itemsOrder.addItems(new ExtraLargeOnionPizza());
  79.                                                    break;
  80.                                               }
  81.                                    }
  82.                                 break;
  83.                           case 3:
  84.                                 {
  85.                                     System.out.println(“You ordered Masala Pizza”);
  86.                                     System.out.println(“Enter the Masala pizza size”);
  87.                                     System.out.println(“————————————“);
  88.                                     System.out.println(”    1. Small Masala Pizza “);
  89.                                     System.out.println(”    2. Medium Masala Pizza “);
  90.                                     System.out.println(”    3. Large Masala Pizza “);
  91.                                     System.out.println(”    4. Extra-Large Masala Pizza “);
  92.                                     System.out.println(“————————————“);
  93.                                                   int masalapizzasize=Integer.parseInt(br.readLine());
  94.                                         switch(masalapizzasize)
  95.                                               {
  96.                                                   case 1:
  97.                                                    itemsOrder.addItems(new SmallMasalaPizza());
  98.                                                    break;
  99.                                                   case 2:
  100.                                                    itemsOrder.addItems(new MediumMasalaPizza());
  101.                                                    break;
  102.                                                   case 3:
  103.                                                    itemsOrder.addItems(new LargeMasalaPizza());
  104.                                                    break;
  105.                                                   case 4:
  106.                                                    itemsOrder.addItems(new ExtraLargeMasalaPizza());
  107.                                                    break;
  108.                                               }
  109.                                 }
  110.                                 break;
  111.                      }
  112.                    }
  113.                    break;// Veg- pizza choice completed.
  114.             case 2:
  115.                    {
  116.                        System.out.println(“You ordered Non-Veg Pizza”);
  117.                        System.out.println(“\n\n”);
  118.                                     System.out.println(“Enter the Non-Veg pizza size”);
  119.                                     System.out.println(“————————————“);
  120.                                     System.out.println(”    1. Small Non-Veg  Pizza “);
  121.                                     System.out.println(”    2. Medium Non-Veg  Pizza “);
  122.                                     System.out.println(”    3. Large Non-Veg  Pizza “);
  123.                                     System.out.println(”    4. Extra-Large Non-Veg  Pizza “);
  124.                                     System.out.println(“————————————“);
  125.                        int nonvegpizzasize=Integer.parseInt(br.readLine());
  126.                         switch(nonvegpizzasize)
  127.                              {
  128.                                   case 1:
  129.                                       itemsOrder.addItems(new SmallNonVegPizza());
  130.                                       break;
  131.                                   case 2:
  132.                                       itemsOrder.addItems(new MediumNonVegPizza());
  133.                                       break;
  134.                                   case 3:
  135.                                       itemsOrder.addItems(new LargeNonVegPizza());
  136.                                       break;
  137.                                   case 4:
  138.                                       itemsOrder.addItems(new ExtraLargeNonVegPizza());
  139.                                       break;
  140.                               }
  141.                        }
  142.                         break;
  143.             default:
  144.             {
  145.                   break;
  146.             }
  147.  }//end of main Switch
  148.                     //continued?..
  149.         System.out.println(” Enter the choice of ColdDrink “);
  150.         System.out.println(“============================”);
  151.         System.out.println(”        1. Pepsi            “);
  152.         System.out.println(”        2. Coke             “);
  153.         System.out.println(”        3. Exit             “);
  154.         System.out.println(“============================”);
  155.                     int coldDrink=Integer.parseInt(br.readLine());
  156.         switch (coldDrink)
  157.             {
  158.                case 1:
  159.                                 {
  160.                                     System.out.println(“You ordered Pepsi “);
  161.                                     System.out.println(“Enter the  Pepsi Size “);
  162.                                     System.out.println(“————————“);
  163.                                     System.out.println(”    1. Small Pepsi “);
  164.                                     System.out.println(”    2. Medium Pepsi “);
  165.                                     System.out.println(”    3. Large Pepsi “);
  166.                                     System.out.println(“————————“);
  167.                                               int pepsisize=Integer.parseInt(br.readLine());
  168.                                     switch(pepsisize)
  169.                                               {
  170.                                                   case 1:
  171.                                                    itemsOrder.addItems(new SmallPepsi());
  172.                                                    break;
  173.                                                   case 2:
  174.                                                    itemsOrder.addItems(new MediumPepsi());
  175.                                                    break;
  176.                                                   case 3:
  177.                                                    itemsOrder.addItems(new LargePepsi());
  178.                                                    break;
  179.                                                 }
  180.                                  }
  181.                                 break;
  182.                          case 2:
  183.                                 {
  184.                                     System.out.println(“You ordered Coke”);
  185.                                     System.out.println(“Enter the Coke Size”);
  186.                                     System.out.println(“————————“);
  187.                                     System.out.println(”    1. Small Coke “);
  188.                                     System.out.println(”    2. Medium Coke  “);
  189.                                     System.out.println(”    3. Large Coke  “);
  190.                                     System.out.println(”    4. Extra-Large Coke “);
  191.                                     System.out.println(“————————“);
  192.                                     int cokesize=Integer.parseInt(br.readLine());
  193.                                     switch(cokesize)
  194.                                               {
  195.                                                   case 1:
  196.                                                    itemsOrder.addItems(new SmallCoke());
  197.                                                    break;
  198.                                                   case 2:
  199.                                                    itemsOrder.addItems(new MediumCoke());
  200.                                                    break;
  201.                                                   case 3:
  202.                                                    itemsOrder.addItems(new LargeCoke());
  203.                                                    break;
  204.                                               }
  205.                                 }
  206.                                 break;
  207.                      default:
  208.                                       {
  209.                                                    break;
  210.                                       }
  211.                                }//End of the Cold-Drink switch
  212.                   return itemsOrder;
  213.           } //End of the preparePizza() method 

Step 15:Create a BuilderDemo class that will use the OrderBuilder class.

File: Prototype.java
  1. import java.io.IOException;
  2. public class BuilderDemo {
  3.     public static void main(String[] args) throws IOException {
  4.         // TODO code application logic here
  5.         OrderBuilder builder=new OrderBuilder();
  6.         OrderedItems orderedItems=builder.preparePizza();
  7.         orderedItems.showItems();
  8.         System.out.println(“\n”);
  9.         System.out.println(“Total Cost : “+ orderedItems.getCost());
  10.     }
  11. }// End of the BuilderDemo class

download this Builder Pattern Example

Output

builderoutput1

builderoutput2

Comments

comments