반응형
팩토리매소드 예제에서 추가된 코드임
public interface PizzaIngredientFactory {
public Dough createDough();
public Sauce createSauce();
public Cheese createCheese();
public Veggies[] createVeggies();
public Pepperoni createPepperoni();
public Clams createClams();
}
public NYPizzaIngredientFactory implements PizzaIngredientFactory {
@Override
public Dough createDough() {
return new ThinCrustdough();
}
@Override
public Sauce createSauce() {
return new MarinaraSauce();
}
@Override
public Cheese createCheese() {
return new ReggianoCheese();
}
@Override
public Veggies[] createVeggies() {
Veggies veggies[] = { new Farlic(), new Onion(), new Mushroom(), new RedPepper() };
return veggies;
}
@Override
public Pepperoni createPepperoni() {
return new SlicedPepperoni();
}
@Override
public Clams createClams() {
return new Freshclams();
}
}
public class ChicagoPizzaingredientFactory implements PizzaIngredientFactory{
@Override
public Dough createDough() {
return new ThickCrustDough();
}
@Override
public Sauce createSauce() {
return new PlumTomatoSauce();
}
@Override
public Cheese createCheese() {
return new MozzarellaCheese();
}
@Override
public Veggies[] createVeggies() {
Veggies veggies[] = { new BlackOlives(), new Spinach(), new EggPlant()};
return veggies;
}
@Override
public Pepperoni createPepperoni() {
return new Slicedpepperoni();
}
@Override
public Clams createClams() {
return new FrozenClam();
}
}
public abstract class Pizza {
String name;
Dough dough;
Sauce sauce;
Veggies veggies[];
Cheese cheese;
Pepperoni pepperoni;
Clams clams;
public abstract void prepare(); //추상 메소드로 변경됨.
public void bake(){
System.out.println("굽기");
}
public void cut(){
System.out.println("자르기");
}
public void box(){
System.out.println("포장하기");
}
public String getname(){
return this.name;
}
}
public class CheesePizza extends Pizza{
PizzaIngredientFactory ingredientFactory;
public CheesePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
this.dough = ingredientFactory.createDough();
this.sauce = ingredientFactory.createSauce();
this.cheese = ingredientFactory.createCheese();
}
}
public class ClamPizza extends Pizza{
PizzaIngredientFactory ingredientFactory;
public ClamPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
this.dough = ingredientFactory.createDough();
this.sauce = ingredientFactory.createSauce();
this.cheese = ingredientFactory.createCheese();
this.clams = ingredientFactory.createClams();
}
}
https://jusungpark.tistory.com/14?category=630296
반응형
'Design Architecture > Design Pattern 예제' 카테고리의 다른 글
Composite 패턴 예제 (0) | 2021.09.28 |
---|---|
Decorator 예제 (0) | 2021.09.28 |
Template Method 예제 (0) | 2021.09.28 |
빌더(Builder) 패턴 예제 (0) | 2021.09.26 |
팩토리 메소드(Factory Method) 패턴 예제 (0) | 2021.09.26 |