Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- ios
- WKWebView
- 스트래터지패턴
- ViewController
- 디자인패턴
- RxSwift
- 이터레이터패턴
- 프록시패턴
- Mobile
- 싱글턴패턴
- 전략패턴
- 어댑터패턴
- Xcode
- 추상팩토리패턴
- Lifecycle
- unowned
- DispatchQueue
- 컴파운드패턴
- 데코레이터패턴
- 파사드패턴
- 상태패턴
- SWIFT
- 팩토리메서드패턴
- 템플릿메서드
- 컴포지트패턴
- 옵저버패턴
- 커맨드패턴
- cocoapods
- 스테이트패턴
- Scenedelegate
Archives
- Today
- Total
ios dev kangwook.
05. Abstract Factory Pattern 본문
서로 관련이 있는 객체들을 묶어서 팩토리 클래스로 만들고, 이러한 팩토리들을 조건에 따라 생성하도록 추상 팩토리를 만들어서 객체를 생성하는 패턴
Abstract Factory Pattern
Abstract Factory Pattern은 구체적인 클래스를 지정하지 않고 관련 객체 또는 종속 객체를 구현하는 인터페이스를 제공한다.
- AbstractProduct : 팩토리 메서드로 생성될 객체의 공통 인터페이스
- ConcreteProduct : 구체적으로 객체가 생성되는 클래스
- AbstractFactory : Product타입을 반환하는 팩토리 메서드를 갖는 클래스의 공통 인터페이스
- ConcreteFactory : 추상 팩토리를 구현하는 구현 클래스
Abstract Factory Pattern 예제
- 피자를 만들 때 사용되는 재료들에 대한 정보
- Abstract Factory인 PizzaIngredientFactory를 만든 후
- NewYorkPizzaIngredientFactory, ChicagoPizzaIngredientFactory 구현
- 그 다음 Product인 Dough, Sauce, Cheese, Clam 인터페이스를 만들고 확장해서
- 구체적 클래스인 ThickCrustDough, ThinCrustDough, , MarinaraSauce, PlumTomatoSauce, ReggianoCheese, MozzarellaCheese, FreshClams, FrozenClams를 구현
- 이 예제에서는 전부 구현하지는 않고 팩토리들을 어떻게 구현해서 적용할 것인가에 대해 초점
- AbstractFactory : PizzaIngredientFactory 클래스
public interface PizzaIngredientFactory {
public Dough createDough();
public Sauce createSauce();
public Cheese createCheese();
public Clam createClams();
}
- ConcreteCreator : NewYorkPizzaIngredientFactory, ChicagoPizzaIngredientFactory 클래스
- 추상 클래스인 createPizza 메서드를 구현
public class NewYorkPizzaIngredientFactory implements PizzaIngredientFactory {
public Dough createDough() {
return new ThinCrustDough();
}
public Sauce createSauce() {
return new MarinaraSauce();
}
public Cheese createCheese() {
return new ReggianoCheese();
}
public Clam createClam() {
return new FreshClams();
}
}
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
public Dough createDough() {
return new ThickCrustDough();
}
public Sauce createSauce() {
return new PlumTomatoSauce();
}
public Cheese createCheese() {
return new MozzarellaCheese();
}
public Clam createClam() {
return new FrozenClams();
}
}
- Pizza 클래스 수정
public abstract class Pizza {
String name;
Dough dough;
Sauce sauce;
Cheese cheese;
Clam clam;
abstract void prepare();
void bake() {
System.out.println("baking~~");
}
void box() {
System.out.println("boxing~~");
}
public void setName(String name) {
this.name = name
}
public String getName() {
return name;
}
}
- Pizza 클래스를 상속받는 CheesePizza, ClamPizza
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 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();
clam = ingredientFactory.createClam();
}
}
- 이러한 피자를 만드는 Client라고 할 수 있는 NewYorkPizzaStore
public class NewYorkPizzaStore extends PizzaStore {
protected Pizza createPizza(String item) {
Pizza pizza = null;
PizzaIngredientFactory ingredientFactory = new NewYorkPizzaIngredientFactory;
if (item.equals("cheese") {
pizza = new CheesePizza(ingredientFactory);
pizza.setName("New York Style Cheese Pizza");
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory);
pizza.setName("New York Style Clam Pizza");
}
return pizza;
결론
- Abstract Factory Pattern은 팩토리 메서드 패턴을 확장한 개념으로 큰 규모의 객체군을 형성하는 생성 패턴(Creational Pattern)
- 장점
- 구체적인 클래스를 분리함으로써 재사용성을 높일 수 있음
- 제품 사이의 일관성을 높임
- 단점
- 새로운 종류의 제품을 제공하기 어려움
→ 팩토리의 구현을 변경해야 해서 모든 서브클래스의 변경을 가져옴 - 즉, 확장성이 떨어짐
- 새로운 종류의 제품을 제공하기 어려움
본문은 Head First Design Pattern (2004) 를 참고하여 공부하고 작성하였음
'Design Pattern' 카테고리의 다른 글
07. Command Pattern (0) | 2022.08.22 |
---|---|
06. Singleton Pattern (0) | 2022.08.21 |
04. Factory Method Pattern (0) | 2022.08.18 |
03. Decorator Pattern (0) | 2022.04.30 |
02. Observer Pattern (0) | 2022.03.13 |
Comments