Skip to content

4주차: 팩토리패턴#4

Merged
guswns3371 merged 1 commit intomainfrom
4week
Nov 20, 2022
Merged

4주차: 팩토리패턴#4
guswns3371 merged 1 commit intomainfrom
4week

Conversation

@guswns3371
Copy link
Member

No description provided.

Comment on lines +5 to +21
public abstract class PizzaStore {

public Pizza orderPizza(Pizza.PizzaType pizzaType) {
final Pizza pizza = createPizza(pizzaType);

pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();

return pizza;
}

// pizza 인스턴스를 만드는 작업은 팩토리 메소드에서 처리하도록 함.
// 구상 클래스마다 서로 다른 피자를 만들기 위해 abstract으로 선언
protected abstract Pizza createPizza(Pizza.PizzaType pizzaType);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PizzaStore의 createPizza() 를 구현하는 방식은 2가지가 있다

  1. 서로 다른 팩토리를 만들고, PizzaStore에서 적당한 팩토리를 사용하는 방법
  2. createPizza() 메소드를 추상 메소드로 선언하고, PizzaStore의 서브 클래스를 구현하는 방법

1번 방식

image

피자 가게와 피자 만드는 과정을 하나로 묶지 못한다. (유연성이 낮음)

2번 방식

image

PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore();

Pizza pizza = nyStore.orderPizza(CHEESE); // 뉴욕 피자 스타일의 치즈 피자
pizza = chicagoStore(CHEESE); // 시카고 피자 스타일의 치즈 피자

Comment on lines +10 to +25
public class ChicagoPizzaStore extends PizzaStore {

@Override
protected Pizza createPizza(Pizza.PizzaType pizzaType) {
Pizza pizza = null;
PizzaIngredientFactory pizzaIngredientFactory = new ChicagoPizzaIngredientFactory();
if (Pizza.PizzaType.CHEESE.equals(pizzaType)) {
pizza = new ChicagoStyleCheesePizza(pizzaIngredientFactory);
pizza.setName("시카고 스타일 치즈 피자");
} else if (Pizza.PizzaType.PEPPERONI.equals(pizzaType)) {
pizza = new ChicagoStylePepperoniPizza(pizzaIngredientFactory);
pizza.setName("시카고 스타일 페퍼로니 피자");
}
return pizza;
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createPizza() 를 피자집 스타일에 맞게 생성한다.

PizzaStore

  • 추상 생산자(abstract creator) 클래스 PizzaStore는 팩토리 메소드(추상 메소드 = createPizza)를 정의한다

구상 PizzaStore

  • ChicagoPizzaStore 와 같은 구상 생산자(concrete creator)는 추상 생산자의 팩토리 메소드를 구현한다

Comment on lines +5 to +14
public abstract class Pizza {
protected String name;
protected Dough dough;
protected Sauce sauce;
protected Veggies[] veggies;
protected Cheese cheese;
protected Pepperoni pepperoni;
protected Clams clam;

public abstract void prepare();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pizza

  • 구상 생산자 클래스(ChicagoPizzaStore, NYPizzaStore)에서 만들어지는 제품 클래스(product class)이다.

팩토리 패턴

image

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

image

Comment on lines +8 to +15
public interface PizzaIngredientFactory {
Dough createDough();
Sauce createSauce();
Cheese createCheese();
Veggies[] createVeggies();
Pepperoni createPepperoni();
Clams createClam();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의존성 뒤집기 원칙(Dependency Inversion Priciple)을 활용하여
PizzaStore(고수준 구성 요소)가 가지는 피자 객체(저수준 구성 요소)에 대한 의존성을 없애기 위해
추상 팩토리 패턴 을 활용한다.

각 PizzStore 구상 클래스는 PizzaIngredientFactory를 통해 생성된 원재료를 세팅해주면 된다.
(PizzaIngredientFactory 또한 피자집의 성격에 맞는 구상 클래스를 구현해야한다.)

추상 팩토리 패턴

구상 클래스에 의존하지 않고도 서로 연관되거나 의존적인 객체로 이루어진 제품군을 생산하는 인터페이스를 제공합니다. 구상 클래스는 서브클래스에서 만듭니다

@guswns3371 guswns3371 merged commit f064bb5 into main Nov 20, 2022
@guswns3371 guswns3371 deleted the 4week branch November 20, 2022 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant