Pages

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 voidprepare() {
           System.out.println("Add Veggie !!");

     }

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

     }

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

     }

     @Override
     public voidpack() {
           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 voidprepare() {
           System.out.println("Add Cheese !!");
          
     }

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

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

     @Override
     public voidpack() {
           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 finalvoidtemplatePizzaPrepare() {
     
      /*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);*/
     }
}

2 comments:

  1. Halo,I'm Helena Julio from Ecuador,I want to talk good about Le_Meridian Funding Investors on this topic.Le_Meridian Funding Investors gives me financial support when all bank in my city turned down my request to grant me a loan of 500,000.00 USD, I tried all i could to get a loan from my banks here in Ecuador but they all turned me down because my credit was low but with god grace I came to know about Le_Meridian so I decided to give a try to apply for the loan. with God willing they grant me  loan of 500,000.00 USD the loan request that my banks here in Ecuador has turned me down for, it was really awesome doing business with them and my business is going well now. Here is Le_Meridian Funding Investment Email/WhatsApp Contact if you wish to apply loan from them.Email:lfdsloans@lemeridianfds.com / lfdsloans@outlook.comWhatsApp Contact:+1-989-394-3740.

    ReplyDelete
  2. Deciding what to eat is always a challenge, especially with Diabetes. However, how much sugar is in a little caesars pizza is definitely one of those foods that everyone loves and craves. The question is, can you have it as part of your diet and still be able to maintain your health? Well, yes, you can, but you need to understand some of the nutritional content of pizza to figure out how to incorporate it into your new and healthier lifestyle.

    ReplyDelete