"new" λ ꡬμ κ°μ²΄λ₯Ό λ»νλ€.
μμμ λ°°μ΄ κ² μ²λΌ ꡬμν΄λμ€λ₯Ό λ°νμΌλ‘ μ½λ©νλ©΄ λμ€μ μ½λλ₯Ό μμ ν΄μΌ ν κ°λ₯μ±μ΄ λμμ§κ³ , μ μ°μ±μ΄ λ¨μ΄μ§κ² λλ€.
Duck duck;
if( picnic) {
duck = new MallardDuck();
} else if( hunting ){
duck = new DecoyDuck();
} else if ( inBathTub){
duck = new RubberDuck();
}
μ΄λ° μ½λλ λκ°λ₯Ό λ°μνκ±°λ, νμ₯ν΄μΌ ν λ μ½λλ₯Ό λ€μ νμΈνκ³ μΆκ° λλ μ κ±° ν΄μΌ νλ€λ κ²μ λ»νλ€. λ°λΌμ μ΄λ° μ½λλ κ΄λ¦¬ λ° κ°±μ μ΄ νλ€μ΄μ§κ³ μ€λ₯κ° μκΈΈ κ°λ₯μ±μ΄ ν¬λ€.
μ ν리μΌμ΄μ μμ ꡬμν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λ§λλ λΆλΆμ μμ λ€λ₯Έμͺ½μΌλ‘ λΆλ¦¬ μμΌμΌ νλ€.
μμμ λ°°μ λ μμΉμ€ "λ°λμ μλ λΆλΆμ μ°Ύμλ΄μ λ°λμ§ μλ λΆλΆκ³Ό λΆλ¦¬μμΌμΌ νλ€." λ₯Ό λ§νλ€.
κ°λ¨ν ν©ν 리 μμ
public Pizza orderPizza( String type){
Pizza pizza;
if( type.equals("cheese")){
pizza = new CheesePizza();
} else if( type.equals("greek")){
pizza = new GreekPizza();
} else if( type.equals("peperoni")){
pizza = new PeperoniPizza();
}
}
μμ μ½λ μ€μ μΈμ€ν΄μ€ μμ± λΆλΆμ λΆλ¦¬ μν€λ©΄, μλμ κ°μ΄ λλ€.
public class SimplePizzaFactory{
pubic Pizza createPizza(String type){
Pizza pizza = null;
if( type.equals("cheese")){
pizza = new CheesePizza();
} else if( type.equals("greek")){
pizza = new GreekPizza();
} else if( type.equals("peperoni")){
pizza = new PeperoniPizza();
}
}
}
μ΄λ° μμΌλ‘ λ°λλ λΆλΆμ λΆλ¦¬μν΄μΌλ‘μ¨ κ΅¬νμ λ³κ²½ν΄μΌ νλ κ²½μ°μ μ¬κΈ°μ κΈ° λ€μ΄κ°μ κ³ μΉ νμ μμ΄
ν©ν 리 ν΄λμ€λ§ λ³κ²½νλ©΄ λλ€.
ν©ν 리 λ©μλ ν¨ν΄
public abstract class PizzaStore{
public Pizza orderPizza(String item){
Pizza pizza;
pizza = createPizza(item);
...
..
return pizza;
}
abstract Pizza createPizza(String item); //ν©ν 리 κΈ°λ₯μ νλ λ©μλ!!
}
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;
}
}
μνΌ ν΄λμ€μ orederPizza() λ©μλμμ μ΄λ€ νΌμκ° μ΄λ»κ² λ§λ€μ΄μ§λμ§ λͺ¨λ₯Έλ€.
μλΈ ν΄λμ€μ createPizza() λ©μλκ° κ²°μ μ νκ² λλ€. -> ν©ν 리 λ©μλμ μ₯μ .
abstract Product factoryMethod ( String type )
νμ abstractλ‘ μ μΈν΄μ μλΈ ν΄λμ€μμ κ°μ²΄μμ±μ μ± μμ§λλ‘ ν΄μΌνλ€.
* μ₯μ ) ν©ν 리λ©μλλ κ°μ²΄ μμ±μ μ²λ¦¬νλ©°, ν©ν 리 λ©μλλ₯Ό μ΄μ©νλ©΄
κ°μ²΄λ₯Ό μμ±νλ μμ μ μλΈν΄λμ€μ μΊ‘μν μν¬μ μλ€.
μ΄λ κ² νλ©΄ μνΌν΄λμ€μ μλ ν΄λΌμ΄μΈνΈ μ½λμ μλΈν΄λμ€μ μλ κ°μ²΄ μμ±μ½λλ₯Ό λΆλ¦¬ μν¬μ μλ€.
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 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");
}
}
//main
PizzaStore nyStore = new NYPizzaStore();
Pizza pizza = nyStore.orderPizza("cheese");
System.out.prinltn("Ethan ordered a " + pizza.getName() );
μΆλ ₯)
--- Making a NY Style Sauce and Cheese Pizza ---
Prepare NY Style Sauce and Cheese Pizza
Tossing dough...
Adding sauce...
Adding toppings:
Grated Reggiano Cheese
Bake for 25 minutes at 350
Cut the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a NY Style Sauce and Cheese Pizza
ν©ν 리 λ©μλ ν¨ν΄ μ μ ) ν©ν 리 λ©μλ ν¨ν΄μμλ κ°μ²΄λ₯Ό μμ±νκΈ° μν μΈν°νμ΄μ€λ₯Ό μ μνλλ°,
μ΄λ€ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λ§λ€μ§λ μλΈν΄λμ€μμ κ²°μ νκ² λ§λ λ€.
ν©ν 리 λ©μλ ν¨ν΄μ μ΄μ©νλ©΄ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λ§λλ μΌμ μλΈν΄λμ€μκ² λ§‘κΈ΄λ€.
μμ‘΄μ± λ€μ§κΈ° μμΉ ) μΆμνλ κ²μ μμ‘΄νκ² λ§λ€μ΄λΌ. ꡬμ ν΄λμ€μ μμ‘΄νλλ‘ λ§λ€μ§ λ§λΌ.
μμ ν΄λμ€ λ€μ΄μ΄κ·Έλ¨μμ PizzaStore μ Pizza , μ μμ€κ΅¬μ±μμμΈ pizzaκ°μ²΄λ€ κ΄κ³λ₯Ό μ΄ν΄λ³΄λ©΄
PizzaStore -> Pizza <- pizza ( ex. NYStyleCheesePizza ...etc)
μ΄λ λ― λͺ¨λ μΆμν΄λμ€μΈ Pizzaμλ§ μμ‘΄νλκ²μ΄ 보μΈλ€.
ν©ν 리 λ©μλ ν¨ν΄μΌλ‘ μΈν΄ μμ‘΄μ± λ€μ§κΈ° μμΉμ μ μ©λ κ²μ΄λ€.
PizzaStoreμλ Pizzaκ°μ²΄λ§ μκ³ , μ μμ€κ΅¬μ±μμ pizzaκ°μ²΄λ€μ
λͺ¨λ μλΈν΄λμ€μμ κ²°μ νκΈ° λλ¬Έμ μ΄λ° κ΄κ³κ° λμ¬μ μλ κ²μ΄λ€.
μΆμ ν©ν 리 ν¨ν΄
public interface PizzaIngredientFactory {
public Dough createDough();
public Sauce createSauce();
public Cheese createCheese();
public Veggies[] createVeggies();
public Pepperoni createPepperoni();
public Clams createClam();
}
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();
}
}
public abstract class Pizza {
String name;
Dough dough;
Sauce sauce;
Veggies veggies[];
Cheese cheese;
Pepperoni pepperoni;
Clams clam;
abstract void prepare();
...
..
}
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();
}
}
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();
}
}
μΆμ ν©ν 리 ν¨ν΄ ) μΆμ ν©ν 리 ν¨ν΄μμλ μΈν°νμ΄μ€λ₯Ό μ΄μ©νμ¬ μλ‘ μ°κ΄λ,
λλ μμ‘΄νλ κ°μ²΄λ₯Ό ꡬμ ν΄λμ€λ₯Ό μ§μ νμ§ μκ³ λ μμ±ν μ μμ΅λλ€.
λ°λΌμ ν΄λΌμ΄μΈνΈμ ν©ν 리μμ μμ°λλ μ νμ λΆλ¦¬μν¬ μ μλ€.