728x90
Overview
추상 팩토리 패턴은 실제 객체가 무엇인지 모른 채 객체를 생성하고 조작하는 데 도움이 될 수 있으므로 객체가 생성, 구성 또는 표현되는 방식에 관계없이 독립적으로 시스템을 생성하는 데 도움이 되기 때문에 사용됩니다.
장점
재사용성은 구체적인 클래스를 분리하여 개선할 수 있습니다.
List와 Iterator가 강하게 연결되어 있다는 점을 감안하면 제품의 일관성을 향상시킬 수 있습니다. (반복자는 List를 사용할 때 항상 사용할 수 있음)
불리
낮은 확장성. 예를 들어 List에서 다른 Iterator 인터페이스를 사용하려는 경우 상속받은 클래스인 ArrayList와 LinkedList를 모두 구현해야 합니다. 또는 ArrayList 에서 Iterator 인터페이스 이외의 인터페이스를 사용하는 것이 어려울 수 있습니다.
Structure
Code Example
package com.example.design.pattern.core;
interface Dough {
public String toString();
}
class ThickCrustDough implements Dough {
public String toString() {
return "ThickCrust style extra thick crust dough";
}
}
class ThinCrustDough implements Dough {
public String toString() {
return "Thin Crust Dough";
}
}
interface Pepperoni {
public String toString();
}
class SlicedPepperoni implements Pepperoni {
public String toString() {
return "Sliced Pepperoni";
}
}
interface PizzaIngredientFactory {
public Dough createDough();
public Pepperoni createPepperoni();
}
class NYPizzaIngredientFactory implements PizzaIngredientFactory {
public Dough createDough() {
return new ThinCrustDough();
}
public Pepperoni createPepperoni() {
return new SlicedPepperoni();
}
}
class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
public Dough createDough() {
return new ThickCrustDough();
}
public Pepperoni createPepperoni() {
return new SlicedPepperoni();
}
}
abstract class Pizza {
String name;
Dough dough;
Pepperoni pepperoni;
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 (pepperoni != null) {
result.append(pepperoni);
result.append("\n");
}
return result.toString();
}
}
class CheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public CheesePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
void prepare() {
System.out.println("Preparing " + name);
dough = ingredientFactory.createDough();
pepperoni = ingredientFactory.createPepperoni();
}
}
class PepperoniPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public PepperoniPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
void prepare() {
System.out.println("Preparing " + name);
dough = ingredientFactory.createDough();
pepperoni = ingredientFactory.createPepperoni();
}
}
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;
}
}
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("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("New York Style Pepperoni Pizza");
}
return pizza;
}
}
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("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("Chicago Style Pepperoni Pizza");
}
return pizza;
}
}
public class MyAbstractFactory {
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("pepperoni");
System.out.println("Ethan ordered a " + pizza + "\n");
pizza = chicagoStore.orderPizza("pepperoni");
System.out.println("Joel ordered a " + pizza + "\n");
}
}
Console Result
--- Making a New York Style Cheese Pizza ---
Preparing New York Style Cheese Pizza
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a ---- New York Style Cheese Pizza ----
Thin Crust Dough
Sliced Pepperoni
--- Making a Chicago Style Cheese Pizza ---
Preparing Chicago Style Cheese Pizza
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Joel ordered a ---- Chicago Style Cheese Pizza ----
ThickCrust style extra thick crust dough
Sliced Pepperoni
--- Making a New York Style Pepperoni Pizza ---
Preparing New York Style Pepperoni Pizza
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a ---- New York Style Pepperoni Pizza ----
Thin Crust Dough
Sliced Pepperoni
--- Making a Chicago Style Pepperoni Pizza ---
Preparing Chicago Style Pepperoni Pizza
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Joel ordered a ---- Chicago Style Pepperoni Pizza ----
ThickCrust style extra thick crust dough
Sliced Pepperoni
Conclusion
N/A
728x90
'Java Design Pattern' 카테고리의 다른 글
[ENG][디자인패턴]Prototype 패턴 이론과 설명 (0) | 2022.06.22 |
---|---|
[ENG][디자인패턴]Proxy 패턴 이론과 설명 (0) | 2022.06.21 |
[ENG][디자인패턴]Facade 패턴 이론과 설명 (0) | 2022.06.20 |
[ENG][디자인패턴]Command 패턴 이론과 설명 (0) | 2022.06.18 |
[ENG][디자인패턴]Composite 패턴 이론과 설명 (0) | 2022.06.07 |
[ENG][디자인패턴]Adapter 패턴 이론과 설명 (0) | 2022.06.06 |
[ENG][디자인패턴]Template Method 패턴 이론과 설명 (0) | 2022.06.04 |
[ENG][디자인패턴]Strategy 패턴 이론과 설명 (0) | 2022.06.03 |