Friday 8 July 2016

Pizza Factory design in Java

Design patterns used:

1.     Abstract Factory,
2.     Template method pattern (to define the preparation sequence).

VeggiePizza.java
package com.pizza.factory;
public class VeggiePizza extends Pizza {

     public VeggiePizza() {
           name = "Veggie Pizza";
           dough = "Crust";
           sauce = "Marinara sauce";
           toppings.add("Shredded mozzarella");
           toppings.add("Grated parmesan");
           toppings.add("Diced onion");
           toppings.add("Sliced mushrooms");
           toppings.add("Sliced red pepper");
           toppings.add("Sliced black olives");
     }

     @Override
     public void prepare() {
           System.out.println("Add Veggie !!");

     }

     @Override
     public void bake() {
           System.out.println("Bake Veggie Pizza !!");

     }

     @Override
     public void cut() {
           System.out.println("Cut Veggie Pizza !!");

     }

     @Override
     public void pack() {
           System.out.println("Pack Veggie Pizza !!");

     }
}

CheesePizza.java
package com.pizza.factory;
public class CheesePizza extends Pizza {
    
     public CheesePizza() {
           name = "Cheese Pizza";
           dough = "Regular Crust";
           sauce = "Marinara Pizza Sauce";
           toppings.add("Fresh Mozzarella");
           toppings.add("Parmesan");
     }

     @Override
     public void prepare() {
           System.out.println("Add Cheese !!");
          
     }

     @Override
     public void bake() {
           System.out.println("Bake Cheese Pizza !!");
          
     }

     @Override
     public void cut() {
           System.out.println("Cut Cheese Pizza !!");
          
     }

     @Override
     public void pack() {
           System.out.println("Pack Cheese Pizza !!");
     }
}

Pizza.java
package com.pizza.factory;
import java.util.ArrayList;

public abstract class Pizza {
     
      String name;
      String dough;
      String sauce;
      ArrayList<String> toppings = new ArrayList<String>();
     
     
    /**
     * Template method Pattern - Sequence of processes.
     * To define the sequence to prepare Pizza.
     */
    public final void templatePizzaPrepare() {
     
      /*Step#1. prepare Pizza integrants */
      prepare();
     
      /*Step#2. bake the Pizza. */
      bake();
     
      /*Step#3. cut the slice of Pizza. */
      cut();
     
      /*Step#4. pack the Pizza. */
      pack();
    }

      public abstract void prepare();
     
      public abstract void bake();
     
      public abstract void cut();
     
      public abstract void pack();
}

SimplePizzaFactory.java
package com.pizza.factory;
public class SimplePizzaFactory {
    
     public static Pizza orderPizza(String type) {
          
           Pizza pizza = null;
          
           if("cheese".equals(type)) {
                pizza = new CheesePizza();
           } else if ("veggie".equals(type)) {
                pizza = new VeggiePizza();
           }
          
           pizza.templatePizzaPrepare();
           return pizza;
     }
}

PizzaStore.java

package com.pizza.factory;
public class PizzaStore {

     public static void main(String[] args) {

           Pizza pizza  = SimplePizzaFactory.orderPizza("cheese");
          
           System.out.println("\nPizza ready : "+ pizza.name);
          
           /*pizza  = SimplePizzaFactory.createPizza("veggie");
           System.out.println("Pizza type : "+ pizza);*/
     }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...