기타/Java

[Java]Design Pattern-Simple Factory

stonesy 2021. 10. 28. 12:32
728x90

new를 모아 simplefactory를 만듦

/*Pizza*/
import java.util.ArrayList;

abstract public class Pizza {
    String name;
    String dough;
    String sauce;
    ArrayList<String> toppings = new ArrayList<String>();

    public String getName() {
        return name;
    }

    public void prepare() {
        System.out.println("Preparing " + name);
    }

    public void bake() {
        System.out.println("Baking " + name);
    }

    public void cut() {
        System.out.println("Cutting " + name);
    }

    public void box() {
        System.out.println("Boxing " + name);
    }

    public String toString() {
        // code to display pizza name and ingredients
        StringBuffer display = new StringBuffer();
        display.append("---- " + name + " ----\n");
        display.append(dough + "\n");
        display.append(sauce + "\n");
        for (String topping : toppings) {
            display.append(topping + "\n");
        }
        return display.toString();
    }
}
/*CheesePizza*/
public class CheesePizza extends Pizza {
    public CheesePizza() {
        name = "Cheese Pizza";
        dough = "Regular Crust";
        sauce = "Marinara Pizza Sauce";
        toppings.add("Fresh Mozzarella");
        toppings.add("Parmesan");
    }
}
/*ClamPizze*/
public class ClamPizza extends Pizza {
    public ClamPizza() {
        name = "Clam Pizza";
        dough = "Thin crust";
        sauce = "White garlic sauce";
        toppings.add("Clams");
        toppings.add("Grated parmesan cheese");
    }
}
/*PepperoniPizza*/
public class PepperoniPizza extends Pizza {
    public PepperoniPizza() {
        name = "Pepperoni Pizza";
        dough = "Crust";
        sauce = "Marinara sauce";
        toppings.add("Sliced Pepperoni");
        toppings.add("Sliced Onion");
        toppings.add("Grated parmesan cheese");
    }
}
/*VeggiePizza*/
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");
    }
}

PizzaStore

public class PizzaStore {
    public Pizza orderPizza(String type){
        Pizza pizza = SimpleFactory.createPizza(type);

        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();

        return pizza;
    }
}

SimpleFactory

public class SimpleFactory {
    public static Pizza createPizza(String type){
        Pizza pizza = null;

        if (type.equals("cheese")) {
            pizza = new CheesePizza();
        } else if (type.equals("pepperoni")) {
            pizza = new PepperoniPizza();
        } else if (type.equals("clam")) {
            pizza = new ClamPizza();
        } else if (type.equals("veggie")) {
            pizza = new VeggiePizza();
        }

        return pizza;
    }
}

Main

public class Main {
    public static void main(String args[]){
        PizzaStore mystore = new PizzaStore();
        Pizza pizza = mystore.orderPizza("cheese");
        System.out.println("We ordered a "+pizza.getName());
        System.out.println(pizza);
    }
}

<실행결과>

Preparing Cheese Pizza
Baking Cheese Pizza
Cutting Cheese Pizza
Boxing Cheese Pizza
We ordered a Cheese Pizza
---- Cheese Pizza ----
Regular Crust
Marinara Pizza Sauce
Fresh Mozzarella
Parmesan

728x90

'기타 > Java' 카테고리의 다른 글

[Design Pattern]Command Pattern  (0) 2021.11.16
[Java]Design Pattern-Singleton  (0) 2021.11.03
[Java]Design Pattern-Decorator Pattern  (0) 2021.10.28
[Java]Design Pattern-Abstract Factory  (0) 2021.10.27
[Java]Design Pattern-Factory Method  (0) 2021.10.25