Design Architecture/Design Pattern 예제

추상 팩토리(Abstract Factory)패턴 예제

lipnus 2021. 9. 26. 14:36
반응형

팩토리매소드 예제에서 추가된 코드임

 

 

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 

 

디자인패턴 - 팩토리 패턴 (factory pattern)

팩토리 패턴 (factory pattern) 팩토리 메소드 패턴 : 객체를 생성하기 위한 인터페이스를 정의하는데, 어떤 클래스의 인스턴스를  만들지는 서브클래스에서 결정하게 만든다. 즉 팩토리 메소드 패

jusungpark.tistory.com

 

반응형

'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