기타/Java

[Java]Design Pattern-Factory Method

stonesy 2021. 10. 25. 13:33
728x90

부품만 교체하고 싶을때

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");
    }
}
/*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");
    }
}
/*PizzaFactory*/
public abstract class PizzaFactory {
    public abstract Pizza getInstance();
    public Pizza preparePizza(){
        Pizza pizza = getInstance();

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

        return pizza;
    }
}
/*CheesePizzaFactory*/
public class CheesePizzaFactory extends PizzaFactory{
    @Override
    public Pizza getInstance(){
        return new CheesePizza();
    }
}
/*VeggiePizzaFactory*/
public class VeggiePizzaFactory extends PizzaFactory{
    @Override
    public Pizza getInstance(){
        return new VeggiePizza();
    }
}
public class Main {
    public static void main(String[] args) {
        PizzaFactory myFactory = new VeggiePizzaFactory();
        Pizza pizza = myFactory.preparePizza();
        System.out.println("We ordered a "+pizza.getName()+"\n");
        System.out.println(pizza);
    }
}

<출력결과>
Marinara sauce
Shredded mozzarella
Grated parmesan
Diced onion
Sliced mushrooms
Sliced red pepper
Sliced black olives


프랜차이즈

/*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();
    }
}
/*PizzaStore*/
public abstract class PizzaStore {
    public Pizza orderPizza(String type) {
        Pizza pizza = createPizza(type);

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

        return pizza;
    }

    public abstract Pizza createPizza(String type);
}
/*NYPizzaStore*/
public class NYPizzaStore extends PizzaStore{
    @Override
    public Pizza createPizza(String type){
        Pizza pizza = null;

        if (type.equals("cheese")) {
            pizza = new NYStyleCheesePizza();
        }else if (type.equals("veggie")) {
            pizza = new NYStyleVeggiePizza();
        }

        return pizza;
    }
}
/*NYStyleCheesePizza*/
public class NYStyleCheesePizza extends Pizza{
    public NYStyleCheesePizza(){
        name = "NY Style Sauce and Cheese Pizza";
        dough = "Thin Crust Dough";
        sauce = "Marinara Sauce";

        toppings.add("Grated Reggiano Cheese");
    }
}
/*NYStyleVeggiePizza*/
public class NYStyleVeggiePizza extends Pizza{
    public NYStyleVeggiePizza(){
        name = "NY Style Veggie Pizza";
        dough = "Thin Crust Dough";
        sauce = "Marinara sauce";

        toppings.add("Grated Reggiano Cheese");
        toppings.add("Garlic");
        toppings.add("Onion");
        toppings.add("Mushrooms");
        toppings.add("Red Pepper");
    }
}
/*ChicagoPizzaStore*/
public class ChicagoPizzaStore extends PizzaStore{
    @Override
    public Pizza createPizza(String type){
        Pizza pizza = null;

        if (type.equals("cheese")) {
            pizza = new ChicagoStyleCheesePizza();
        }else if (type.equals("veggie")) {
            pizza = new ChicagoStyleVeggiePizza();
        }

        return pizza;
    }
}
/*ChicagoStyleCheesePizza*/
public class ChicagoStyleCheesePizza extends Pizza{
    public ChicagoStyleCheesePizza(){
        name = "Chicago Style Deep Dish Cheese Pizza";
        dough = "Extra Thick Crush Dough";
        sauce = "Plum Tomato Sauce";

        toppings.add("Shredded Mozzarella Cheese");
    }
    public void cut(){
        System.out.println("Cutting the pizza into square slices");
    }
}
/*ChicagoStyleVeggiePizza*/
public class ChicagoStyleVeggiePizza extends Pizza{
    public ChicagoStyleVeggiePizza(){
        name="Chicago Style Veggie Pizza";
        dough="Extra Thick Crust Dough";
        sauce="Plum Tomato Sauce";

        toppings.add("Shredded Mozzarella Cheese");
        toppings.add("Black Olives");
        toppings.add("Spinach");
        toppings.add("Eggplant");
    }
    public void cut(){
        System.out.println("Cutting the pizza into square slices");
    }
}
/*Main*/
public class Main {
    public static void main(String[] args){
        PizzaStore store = new NYPizzaStore();

        Pizza pizza = store.orderPizza("cheese");
        System.out.println("We ordered a "+pizza.getName()+"\n");
        System.out.println(pizza);

        pizza = store.orderPizza("veggie");
        System.out.println("We ordered a "+pizza.getName()+"\n");
        System.out.println(pizza);
    }
}

<출력결과>

Preparing NY Style Sauce and Cheese Pizza
Baking NY Style Sauce and Cheese Pizza
Cutting NY Style Sauce and Cheese Pizza
Boxing NY Style Sauce and Cheese Pizza
We ordered a NY Style Sauce and Cheese Pizza

---- NY Style Sauce and Cheese Pizza ----
Thin Crust Dough
Marinara Sauce
Grated Reggiano Cheese

Preparing NY Style Veggie Pizza
Baking NY Style Veggie Pizza
Cutting NY Style Veggie Pizza
Boxing NY Style Veggie Pizza
We ordered a NY Style Veggie Pizza

---- NY Style Veggie Pizza ----
Thin Crust Dough
Marinara sauce
Grated Reggiano Cheese
Garlic
Onion
Mushrooms
Red Pepper

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-Simple Factory  (0) 2021.10.28
[Java]Design Pattern-Abstract Factory  (0) 2021.10.27