1. 정의
- 구체적인 클래스에 의존하지 않고도 서로 연관되거나 의존적인 객체로 이루어진 군집을 생성하는 인터페이스를 제공한다. 구체적인 클래스는 서브클래스에서 생성한다.
💡 관련성이 있는 객체를 일관된 방식으로 생성할 수 있는 디자인 패턴
2. 예시
- 서울과 제주에 피자 체인점을 낸다.
3. 그림
4. 클래스 다이어그램
5. 코드
public class Client {
public static void main(String[] args){
PizzaStore seoul = new SeoulPizzaStore();
System.out.println("------");
PizzaStore jeju = new JejuPizzaStore();
}
}
public abstract class Pizza {
String description;
void getDescription(){
System.out.println(this.description);
}
}
public abstract class PizzaStore {
abstract Pizza createPizza(String type);
public Pizza orderPizza(String type){
Pizza pizza = createPizza(type);
return pizza;
}
}
public class SeoulPizzaStore extends PizzaStore {
@Override
Pizza createPizza(String type) {
return null;
}
}
public class JejuPizzaStore extends PizzaStore {
@Override
Pizza createPizza(String type) {
return null;
}
}
https://github.com/kang-seongbeom/design_pattern
💡 Github에서 디자인 패턴 코드를 볼 수 있습니다.
6. 설명
- 체인점 SeoulPizzaStore과 JejuPizzaStore은 본사인 PizzaStore을 상속한다.
- 즉, SeoulPizzaStore와 JejuPizzaStore는 PizzaStore군집의 일부다.
- Client는 PizzaStore 군집을 통해 구체적인 클래스에 의존하지 않고, 일관된 방식으로 객체를 생성할 수 있다.
- 구체적인 클래스는 SeoulPizzaStore또는 JejuPizzaStore가 생성한다.
💡 어떤 서브클래스를 사용하는냐에 따라 생성되는 객체가 달라진다.
💡 Client는 참조변수를 PizzaStore로 사용하기 때문에 런타임시 객체가 주입되도 상관없다.
'디자인 패턴' 카테고리의 다른 글
디자인 패턴 - 싱글톤 패턴(Singleton Pattern) (0) | 2022.11.17 |
---|---|
디자인 패턴 - 팩토리 메소드 패턴(Factory Method Pattern) (0) | 2022.11.17 |
디자인 패턴 - 데코레이터 패턴(Decorator Pattern) (0) | 2022.11.17 |
디자인 패턴 - 옵저버 패턴(Observer Pattern) (0) | 2022.11.17 |
디자인 패턴 - 전략 패턴(Strategy Pattern) (0) | 2022.11.10 |