자바/자바 자료실

[디자인 패턴] 04. 팩토리 패턴 (Factory Pattern)

Chipmunks 2018. 10. 23.
728x90


팩토리 메소드 패턴 (Factory Metgo Pattern)

팩토리 메소드 패턴에서는 객체를 생성하기 위한 인터페이스를 정의하는데, 어떤 클래스의 인스턴스를 만들지는 서브클래스에서 결정하게 만듭니다. 팩토리 메소드 패턴을 이용하면 클래스의 인스턴스를 만드는 일을 서브스클래스에게 맡긴다.


특징

  1. 어떤 구상 클래스가 생성되는지 상관 없이 로직을 구현할 수 있다.
  2. 자식 클래스에게 객체 생성 책임을 부여한다.
  3. 런타임에 어느 구상 객체가 생성되는지 결정하지는 않는다.


다이어그램



팩토리 메소드 패턴 예제




추상 팩토리 패턴 (Abstract Factory Pattern)

추상 팩토리 패턴에서는 인터페이스를 이용하여 서로 연관된, 또는 의존하는 객체를 구상 클래스를 지정하지 않고도 생성할 수 있습니다.


장점

  1. 클라이언트와 구상 클래스 구현을 분리시킵니다.
  2. 제품군을 쉽게 바꿀 수 있다.
  3. 일관된 제품군 인터페이스를 제공한다.

단점

  1. 새로운 제품군을 수정하기가 어렵다.


다이어그램




객체지향 원칙

의존성 뒤집기 원칙 (Dependency Inversion Principle)

추상화된 것에 의존하도록 만들자. 구상 클래스에 의존하도록 만들지 말자.
고수준 구성요소가 저수준 구성요소의 의존하면 안 된다.


예제 코드

간단한 팩토리 예제

- pizzas/Pizza.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package pizzas;
 
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();
    }
}
 
 
cs

- pizzas/CheesePizza.java


1
2
3
4
5
6
7
8
9
10
11
12
package pizzas;
 
public class CheesePizza extends Pizza {
    public CheesePizza() {
        name = "Cheese Pizza";
        dough = "Regular Crust";
        sauce = "Marinara Pizza Sauce";
        toppings.add("Fresh Mozzarella");
        toppings.add("Parmesan");
    }
}
 
cs


- pizzas/ClamPizza.java


1
2
3
4
5
6
7
8
9
10
11
12
package pizzas;
 
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");
    }
}
 
cs


- pizzas/PepperoniPizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
package pizzas;
 
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");
    }
}
 
cs


- pizzas/VeggiePizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package pizzas;
 
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");
    }
}
 
cs


- pizzas/SimplePizzaFactory.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package pizzas;
 
public class SimplePizzaFactory {
 
    public 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;
    }
}
 
cs


- pizzas/PizzaStore.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package pizzas;
 
public class PizzaStore {
    SimplePizzaFactory factory;
 
    public PizzaStore(SimplePizzaFactory factory) { 
        this.factory = factory;
    }
 
    public Pizza orderPizza(String type) {
        Pizza pizza;
 
        pizza = factory.createPizza(type);
 
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
 
        return pizza;
    }
 
}
 
cs


- pizzas/PizzaTestDrive.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package pizzas;
 
public class PizzaTestDrive {
 
    public static void main(String[] args) {
        SimplePizzaFactory factory = new SimplePizzaFactory();
        PizzaStore store = new PizzaStore(factory);
 
        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);
    }
}
 
cs


팩토리 메소드 패턴

- pizzafm/Pizza.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package pizzafm;
 
import java.util.ArrayList;
 
public abstract class Pizza {
    String name;
    String dough;
    String sauce;
    ArrayList<String> toppings = new ArrayList<String>();
 
    void prepare() {
        System.out.println("Prepare " + name);
        System.out.println("Tossing dough...");
        System.out.println("Adding sauce...");
        System.out.println("Adding toppings: ");
        for (String topping : toppings) {
            System.out.println("   " + topping);
        }
    }
  
    void bake() {
        System.out.println("Bake for 25 minutes at 350");
    }
 
    void cut() {
        System.out.println("Cut the pizza into diagonal slices");
    }
  
    void box() {
        System.out.println("Place pizza in official PizzaStore box");
    }
 
    public String getName() {
        return name;
    }
 
    public String toString() {
        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();
    }
}
 
 
 
 
cs


- pizzafm/PizzaStore.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package pizzafm;
 
public abstract class PizzaStore {
 
    abstract Pizza createPizza(String item);
 
    public Pizza orderPizza(String type) {
        Pizza pizza = createPizza(type);
        System.out.println("--- Making a " + pizza.getName() + " ---");
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
        return pizza;
    }
}
 
cs


- pizzafm/ChicagoStyleCheesePizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package pizzafm;
 
public class ChicagoStyleCheesePizza extends Pizza {
 
    public ChicagoStyleCheesePizza() { 
        name = "Chicago Style Deep Dish Cheese Pizza";
        dough = "Extra Thick Crust Dough";
        sauce = "Plum Tomato Sauce";
 
        toppings.add("Shredded Mozzarella Cheese");
    }
 
    void cut() {
        System.out.println("Cutting the pizza into square slices");
    }
}
 
cs


- pizzafm/ChicagoStyleClamPizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package pizzafm;
 
public class ChicagoStyleClamPizza extends Pizza {
    public ChicagoStyleClamPizza() {
        name = "Chicago Style Clam Pizza";
        dough = "Extra Thick Crust Dough";
        sauce = "Plum Tomato Sauce";
 
        toppings.add("Shredded Mozzarella Cheese");
        toppings.add("Frozen Clams from Chesapeake Bay");
    }
 
    void cut() {
        System.out.println("Cutting the pizza into square slices");
    }
}
 
cs


- pizzafm/ChicagoStylePepperoniPizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package pizzafm;
 
public class ChicagoStylePepperoniPizza extends Pizza {
    public ChicagoStylePepperoniPizza() {
        name = "Chicago Style Pepperoni 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");
        toppings.add("Sliced Pepperoni");
    }
 
    void cut() {
        System.out.println("Cutting the pizza into square slices");
    }
}
 
cs


- pizzafm/ChicagoStyleVeggiePizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package pizzafm;
 
public class ChicagoStyleVeggiePizza extends Pizza {
    public ChicagoStyleVeggiePizza() {
        name = "Chicago Deep Dish 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");
    }
 
    void cut() {
        System.out.println("Cutting the pizza into square slices");
    }
}
 
cs


- pizzafm/ChicagoPizzaStore.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package pizzafm;
 
public class ChicagoPizzaStore extends PizzaStore {
 
    Pizza createPizza(String item) {
            if (item.equals("cheese")) {
                    return new ChicagoStyleCheesePizza();
            } else if (item.equals("veggie")) {
                    return new ChicagoStyleVeggiePizza();
            } else if (item.equals("clam")) {
                    return new ChicagoStyleClamPizza();
            } else if (item.equals("pepperoni")) {
                    return new ChicagoStylePepperoniPizza();
            } else return null;
    }
}
 
cs


- pizzafm/NYStyleCheesePizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
package pizzafm;
 
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");
    }
}
 
cs


- pizzafm/NYStyleClamPizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
package pizzafm;
 
public class NYStyleClamPizza extends Pizza {
 
    public NYStyleClamPizza() {
        name = "NY Style Clam Pizza";
        dough = "Thin Crust Dough";
        sauce = "Marinara Sauce";
 
        toppings.add("Grated Reggiano Cheese");
        toppings.add("Fresh Clams from Long Island Sound");
    }
}
 
cs


- pizzafm/NYStylePepperoniPizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package pizzafm;
 
public class NYStylePepperoniPizza extends Pizza {
 
    public NYStylePepperoniPizza() {
        name = "NY Style Pepperoni Pizza";
        dough = "Thin Crust Dough";
        sauce = "Marinara Sauce";
 
        toppings.add("Grated Reggiano Cheese");
        toppings.add("Sliced Pepperoni");
        toppings.add("Garlic");
        toppings.add("Onion");
        toppings.add("Mushrooms");
        toppings.add("Red Pepper");
    }
}
 
cs


- pizzafm/NYStyleVeggiePizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package pizzafm;
 
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");
    }
}
 
cs


- pizzafm/NYPizzaStore.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package pizzafm;
 
public class NYPizzaStore extends PizzaStore {
 
    Pizza createPizza(String item) {
        if (item.equals("cheese")) {
            return new NYStyleCheesePizza();
        } else if (item.equals("veggie")) {
            return new NYStyleVeggiePizza();
        } else if (item.equals("clam")) {
            return new NYStyleClamPizza();
        } else if (item.equals("pepperoni")) {
            return new NYStylePepperoniPizza();
        } else return null;
    }
}
 
cs


- pizzafm/PizzaTestDrive.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package pizzafm;
 
public class PizzaTestDrive {
 
    public static void main(String[] args) {
        PizzaStore nyStore = new NYPizzaStore();
        PizzaStore chicagoStore = new ChicagoPizzaStore();
 
        Pizza pizza = nyStore.orderPizza("cheese");
        System.out.println("Ethan ordered a " + pizza.getName() + "\n");
 
        pizza = chicagoStore.orderPizza("cheese");
        System.out.println("Joel ordered a " + pizza.getName() + "\n");
 
        pizza = nyStore.orderPizza("clam");
        System.out.println("Ethan ordered a " + pizza.getName() + "\n");
 
        pizza = chicagoStore.orderPizza("clam");
        System.out.println("Joel ordered a " + pizza.getName() + "\n");
 
        pizza = nyStore.orderPizza("pepperoni");
        System.out.println("Ethan ordered a " + pizza.getName() + "\n");
 
        pizza = chicagoStore.orderPizza("pepperoni");
        System.out.println("Joel ordered a " + pizza.getName() + "\n");
 
        pizza = nyStore.orderPizza("veggie");
        System.out.println("Ethan ordered a " + pizza.getName() + "\n");
 
        pizza = chicagoStore.orderPizza("veggie");
        System.out.println("Joel ordered a " + pizza.getName() + "\n");
    }
}
 
cs


- pizzafm/DependentPizzaStore.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package pizzafm;
 
public class DependentPizzaStore {
 
    public Pizza createPizza(String style, String type) {
        Pizza pizza = null;
        if (style.equals("NY")) {
            if (type.equals("cheese")) {
                pizza = new NYStyleCheesePizza();
            } else if (type.equals("veggie")) {
                pizza = new NYStyleVeggiePizza();
            } else if (type.equals("clam")) {
                pizza = new NYStyleClamPizza();
            } else if (type.equals("pepperoni")) {
                pizza = new NYStylePepperoniPizza();
            }
        } else if (style.equals("Chicago")) {
            if (type.equals("cheese")) {
                pizza = new ChicagoStyleCheesePizza();
            } else if (type.equals("veggie")) {
                pizza = new ChicagoStyleVeggiePizza();
            } else if (type.equals("clam")) {
                pizza = new ChicagoStyleClamPizza();
            } else if (type.equals("pepperoni")) {
                pizza = new ChicagoStylePepperoniPizza();
            }
        } else {
            System.out.println("Error: invalid type of pizza");
            return null;
        }
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
        return pizza;
    }
}
 
cs


추상 팩토리 패턴

- pizzaaf/Dough.java

1
2
3
4
5
6
package pizzaaf;
 
public interface Dough {
    public String toString();
}
 
cs


- pizzaaf/ThickCrustDough.java


1
2
3
4
5
6
7
8
package pizzaaf;
 
public class ThickCrustDough implements Dough {
    public String toString() {
        return "ThickCrust style extra thick crust dough";
    }
}
 
cs


- pizzaaf/ThinCrustDough.java


1
2
3
4
5
6
7
8
package pizzaaf;
 
public class ThinCrustDough implements Dough {
    public String toString() {
        return "Thin Crust Dough";
    }
}
 
cs


- pizzaaf/Clams.java


1
2
3
4
5
6
package pizzaaf;
 
public interface Clams {
    public String toString();
}
 
cs


- pizzaaf/FrozenClams.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class FrozenClams implements Clams {
 
    public String toString() {
        return "Frozen Clams from Chesapeake Bay";
    }
}
 
cs


- pizzaaf/FreshClams.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class FreshClams implements Clams {
 
    public String toString() {
        return "Fresh Clams from Long Island Sound";
    }
}
 
cs


- pizzaaf/Sauce.java


1
2
3
4
5
6
package pizzaaf;
 
public interface Sauce {
    public String toString();
}
 
cs


- pizzaaf/PlumTomatoSauce.java


1
2
3
4
5
6
7
8
package pizzaaf;
 
public class PlumTomatoSauce implements Sauce {
    public String toString() {
        return "Tomato sauce with plum tomatoes";
    }
}
 
cs


- pizzaaf/MarinaraSauce.java


1
2
3
4
5
6
7
8
package pizzaaf;
 
public class MarinaraSauce implements Sauce {
    public String toString() {
        return "Marinara Sauce";
    }
}
 
cs


- pizzaaf/Cheese.java


1
2
3
4
5
6
package pizzaaf;
 
public interface Cheese {
    public String toString();
}
 
cs


- pizzaaf/MozzarellaCheese.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class MozzarellaCheese implements Cheese {
 
    public String toString() {
        return "Shredded Mozzarella";
    }
}
 
cs


- pizzaaf/ReggianoCheese.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class ReggianoCheese implements Cheese {
 
    public String toString() {
        return "Reggiano Cheese";
    }
}
 
cs


- pizzaaf/ParmesanCheese.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class ParmesanCheese implements Cheese {
 
    public String toString() {
        return "Shredded Parmesan";
    }
}
 
cs


- pizzaaf/Pepperoni.java


1
2
3
4
5
6
package pizzaaf;
 
public interface Pepperoni {
    public String toString();
}
 
cs


- pizzaaf/SlicedPepperoni.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class SlicedPepperoni implements Pepperoni {
 
    public String toString() {
        return "Sliced Pepperoni";
    }
}
 
cs


- pizzaaf/Veggies.java


1
2
3
4
5
6
package pizzaaf;
 
public interface Veggies {
    public String toString();
}
 
cs


- pizzaaf/BlackOlives.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class BlackOlives implements Veggies {
 
    public String toString() {
        return "Black Olives";
    }
}
 
cs


- pizzaaf/Eggplant.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class Eggplant implements Veggies {
 
    public String toString() {
        return "Eggplant";
    }
}
 
cs


- pizzaaf/Garlic.java


1
2
3
4
5
6
7
8
9
package pizzaaf;    
 
public class Garlic implements Veggies {
 
    public String toString() {
        return "Garlic";
    }
}
 
cs


- pizzaaf/Mushroom.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class Mushroom implements Veggies {
 
    public String toString() {
        return "Mushrooms";
    }
}
 
cs


- pizzaaf/Onion.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class Onion implements Veggies {
 
    public String toString() {
        return "Onion";
    }
}
 
cs


- pizzaaf/RedPepper.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class RedPepper implements Veggies {
 
    public String toString() {
        return "Red Pepper";
    }
}
 
cs


- pizzaaf/Spinach.java


1
2
3
4
5
6
7
8
9
package pizzaaf;
 
public class Spinach implements Veggies {
 
    public String toString() {
        return "Spinach";
    }
}
 
cs


- pizzaaf/PizzaIngredientFactory.java


1
2
3
4
5
6
7
8
9
10
11
12
13
package pizzaaf;
 
public interface PizzaIngredientFactory {
 
    public Dough createDough();
    public Sauce createSauce();
    public Cheese createCheese();
    public Veggies[] createVeggies();
    public Pepperoni createPepperoni();
    public Clams createClam();
 
}
 
cs


- pizzaaf/NYPizzaIngredientFactory.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package pizzaaf;
 
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {
 
    public Dough createDough() {
        return new ThinCrustDough();
    }
 
    public Sauce createSauce() {
        return new MarinaraSauce();
    }
 
    public Cheese createCheese() {
        return new ReggianoCheese();
    }
 
    public Veggies[] createVeggies() {
        Veggies veggies[] = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() };
        return veggies;
    }
 
    public Pepperoni createPepperoni() {
        return new SlicedPepperoni();
    }
 
    public Clams createClam() {
        return new FreshClams();
    }
}
 
cs


- pizzaaf/ChicagoPizzaIngredientFactory.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package pizzaaf;
 
public class ChicagoPizzaIngredientFactory 
    implements PizzaIngredientFactory 
{
 
    public Dough createDough() {
        return new ThickCrustDough();
    }
 
    public Sauce createSauce() {
        return new PlumTomatoSauce();
    }
 
    public Cheese createCheese() {
        return new MozzarellaCheese();
    }
 
    public Veggies[] createVeggies() {
        Veggies veggies[] = { new BlackOlives(), 
                              new Spinach(), 
                              new Eggplant() };
        return veggies;
    }
 
    public Pepperoni createPepperoni() {
        return new SlicedPepperoni();
    }
 
    public Clams createClam() {
        return new FrozenClams();
    }
}
 
cs


- pizzaaf/Pizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package pizzaaf;
 
public abstract class Pizza {
    String name;
 
    Dough dough;
    Sauce sauce;
    Veggies veggies[];
    Cheese cheese;
    Pepperoni pepperoni;
    Clams clam;
 
    abstract void prepare();
 
    void bake() {
        System.out.println("Bake for 25 minutes at 350");
    }
 
    void cut() {
        System.out.println("Cutting the pizza into diagonal slices");
    }
 
    void box() {
        System.out.println("Place pizza in official PizzaStore box");
    }
 
    void setName(String name) {
        this.name = name;
    }
 
    String getName() {
        return name;
    }
 
    public String toString() {
        StringBuffer result = new StringBuffer();
        result.append("---- " + name + " ----\n");
        if (dough != null) {
            result.append(dough);
            result.append("\n");
        }
        if (sauce != null) {
            result.append(sauce);
            result.append("\n");
        }
        if (cheese != null) {
            result.append(cheese);
            result.append("\n");
        }
        if (veggies != null) {
            for (int i = 0; i < veggies.length; i++) {
                result.append(veggies[i]);
                if (i < veggies.length-1) {
                    result.append(", ");
                }
            }
            result.append("\n");
        }
        if (clam != null) {
            result.append(clam);
            result.append("\n");
        }
        if (pepperoni != null) {
            result.append(pepperoni);
            result.append("\n");
        }
        return result.toString();
    }
}
 
cs


- pizzaaf/CheesePizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package pizzaaf;
 
public class CheesePizza extends Pizza {
    PizzaIngredientFactory ingredientFactory;
 
    public CheesePizza(PizzaIngredientFactory ingredientFactory) {
        this.ingredientFactory = ingredientFactory;
    }
 
    void prepare() {
        System.out.println("Preparing " + name);
        dough = ingredientFactory.createDough();
        sauce = ingredientFactory.createSauce();
        cheese = ingredientFactory.createCheese();
    }
}
 
cs


- pizzaaf/ClamPizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package pizzaaf;
 
public class ClamPizza extends Pizza {
    PizzaIngredientFactory ingredientFactory;
 
    public ClamPizza(PizzaIngredientFactory ingredientFactory) {
        this.ingredientFactory = ingredientFactory;
    }
 
    void prepare() {
        System.out.println("Preparing " + name);
        dough = ingredientFactory.createDough();
        sauce = ingredientFactory.createSauce();
        cheese = ingredientFactory.createCheese();
        clam = ingredientFactory.createClam();
    }
}
 
cs


- pizzaaf/PepperoniPizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package pizzaaf;
 
public class PepperoniPizza extends Pizza {
    PizzaIngredientFactory ingredientFactory;
 
    public PepperoniPizza(PizzaIngredientFactory ingredientFactory) {
        this.ingredientFactory = ingredientFactory;
    }
 
    void prepare() {
        System.out.println("Preparing " + name);
        dough = ingredientFactory.createDough();
        sauce = ingredientFactory.createSauce();
        cheese = ingredientFactory.createCheese();
        veggies = ingredientFactory.createVeggies();
        pepperoni = ingredientFactory.createPepperoni();
    }
}
 
cs


- pizzaaf/VeggiePizza.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package pizzaaf;
 
public class VeggiePizza extends Pizza {
    PizzaIngredientFactory ingredientFactory;
 
    public VeggiePizza(PizzaIngredientFactory ingredientFactory) {
        this.ingredientFactory = ingredientFactory;
    }
 
    void prepare() {
        System.out.println("Preparing " + name);
        dough = ingredientFactory.createDough();
        sauce = ingredientFactory.createSauce();
        cheese = ingredientFactory.createCheese();
        veggies = ingredientFactory.createVeggies();
    }
}
 
cs


- pizzaaf/PizzaStore.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package pizzaaf;
 
public abstract class PizzaStore {
 
    protected abstract Pizza createPizza(String item);
 
    public Pizza orderPizza(String type) {
        Pizza pizza = createPizza(type);
        System.out.println("--- Making a " + pizza.getName() + " ---");
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
        return pizza;
    }
}
 
cs


- pizzaaf/NYPizzaStore.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package pizzaaf;
 
public class NYPizzaStore extends PizzaStore {
 
    protected Pizza createPizza(String item) {
        Pizza pizza = null;
        PizzaIngredientFactory ingredientFactory = 
            new NYPizzaIngredientFactory();
 
        if (item.equals("cheese")) {
  
            pizza = new CheesePizza(ingredientFactory);
            pizza.setName("New York Style Cheese Pizza");
  
        } else if (item.equals("veggie")) {
 
            pizza = new VeggiePizza(ingredientFactory);
            pizza.setName("New York Style Veggie Pizza");
 
        } else if (item.equals("clam")) {
 
            pizza = new ClamPizza(ingredientFactory);
            pizza.setName("New York Style Clam Pizza");
 
        } else if (item.equals("pepperoni")) {
 
            pizza = new PepperoniPizza(ingredientFactory);
            pizza.setName("New York Style Pepperoni Pizza");
 
        } 
        return pizza;
    }
}
 
cs


- pizzaaf/ChicagoPizzaStore.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package pizzaaf;
 
public class ChicagoPizzaStore extends PizzaStore {
 
    protected Pizza createPizza(String item) {
        Pizza pizza = null;
        PizzaIngredientFactory ingredientFactory =
        new ChicagoPizzaIngredientFactory();
 
        if (item.equals("cheese")) {
 
            pizza = new CheesePizza(ingredientFactory);
            pizza.setName("Chicago Style Cheese Pizza");
 
        } else if (item.equals("veggie")) {
 
            pizza = new VeggiePizza(ingredientFactory);
            pizza.setName("Chicago Style Veggie Pizza");
 
        } else if (item.equals("clam")) {
 
            pizza = new ClamPizza(ingredientFactory);
            pizza.setName("Chicago Style Clam Pizza");
 
        } else if (item.equals("pepperoni")) {
 
            pizza = new PepperoniPizza(ingredientFactory);
            pizza.setName("Chicago Style Pepperoni Pizza");
 
        }
        return pizza;
    }
}
 
cs


- pizzaaf/PizzaTestDrive.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package pizzaaf;
 
public class PizzaTestDrive {
 
    public static void main(String[] args) {
        PizzaStore nyStore = new NYPizzaStore();
        PizzaStore chicagoStore = new ChicagoPizzaStore();
 
        Pizza pizza = nyStore.orderPizza("cheese");
        System.out.println("Ethan ordered a " + pizza + "\n");
 
        pizza = chicagoStore.orderPizza("cheese");
        System.out.println("Joel ordered a " + pizza + "\n");
 
        pizza = nyStore.orderPizza("clam");
        System.out.println("Ethan ordered a " + pizza + "\n");
 
        pizza = chicagoStore.orderPizza("clam");
        System.out.println("Joel ordered a " + pizza + "\n");
 
        pizza = nyStore.orderPizza("pepperoni");
        System.out.println("Ethan ordered a " + pizza + "\n");
 
        pizza = chicagoStore.orderPizza("pepperoni");
        System.out.println("Joel ordered a " + pizza + "\n");
 
        pizza = nyStore.orderPizza("veggie");
        System.out.println("Ethan ordered a " + pizza + "\n");
 
        pizza = chicagoStore.orderPizza("veggie");
        System.out.println("Joel ordered a " + pizza + "\n");
    }
}
 
cs


댓글