Pattern - No. 06 디자인 모델 의 공장 모델

30834 단어
1. 간단 한 공장 모델: 정적 공장 방법 모델 이 라 고도 부 르 는데 그 역할 은 창설 대상 의 인 터 페 이 스 를 정의 하 는 것 이다
package com.shma.factory.simplefactory.pizza;

import java.util.ArrayList;
import java.util.List;

/**
 *             
 * @author admin
 *
 */
public abstract class Pizza {
	protected String name;
	protected String dough;
	protected String sauce;
	protected List<String> toppings = new ArrayList<String>();

	public String getName() {
		return name;
	}

	public void prepare() {
		System.out.println("Preparing " + name);
	}

	public void bake() {
		System.out.println("Baking " + name);
	}

	public void cut() {
		System.out.println("Cutting " + name);
	}

	public void box() {
		System.out.println("Boxing " + name);
	}

	public String toString() {

		StringBuffer display = new StringBuffer();
		display.append("---- " + name + " ----
"); display.append(dough + "
"); display.append(sauce + "
"); for (int i = 0; i < toppings.size(); i++) { display.append((String )toppings.get(i) + "
"); } return display.toString(); } } package com.shma.factory.simplefactory.pizza; /**  *   * @author admin  *  */ public enum PizzaType { CHESS, CLAM, PEPPERONI, VEGGIE } package com.shma.factory.simplefactory.pizza; /**  *   * @author admin  *  */ public class CheesePizza extends Pizza { public CheesePizza() { name = "Cheese Pizza"; dough = "Regular Crust"; sauce = "Marinara Pizza Sauce"; toppings.add("Fresh Mozzarella"); toppings.add("Parmesan"); } } package com.shma.factory.simplefactory.pizza; /**  *   * @author admin  *  */ public class ClamPizza extends Pizza { public ClamPizza() { name = "Clam Pizza"; dough = "Thin crust"; sauce = "White garlic sauce"; toppings.add("Clams"); toppings.add("Grated parmesan cheese"); } } package com.shma.factory.simplefactory.pizza; /**  *   * @author admin  *  */ public class PepperoniPizza extends Pizza { public PepperoniPizza() { name = "Pepperoni Pizza"; dough = "Crust"; sauce = "Marinara sauce"; toppings.add("Sliced Pepperoni"); toppings.add("Sliced Onion"); toppings.add("Grated parmesan cheese"); } } package com.shma.factory.simplefactory.pizza; /**  *   * @author admin  *  */ public class VeggiePizza extends Pizza { public VeggiePizza() { name = "Veggie Pizza"; dough = "Crust"; sauce = "Marinara sauce"; toppings.add("Shredded mozzarella"); toppings.add("Grated parmesan"); toppings.add("Diced onion"); toppings.add("Sliced mushrooms"); toppings.add("Sliced red pepper"); toppings.add("Sliced black olives"); } } package com.shma.factory.simplefactory; import com.shma.factory.simplefactory.pizza.CheesePizza; import com.shma.factory.simplefactory.pizza.ClamPizza; import com.shma.factory.simplefactory.pizza.PepperoniPizza; import com.shma.factory.simplefactory.pizza.Pizza; import com.shma.factory.simplefactory.pizza.PizzaType; import com.shma.factory.simplefactory.pizza.VeggiePizza; /**  *   * @author admin  *  */ public class SimplePizzaFactory { public Pizza createPizza(PizzaType type) { Pizza pizza = null; switch(type) { case CHESS : pizza = new CheesePizza(); break; case CLAM : pizza = new ClamPizza(); break; case PEPPERONI : pizza = new PepperoniPizza(); break; case VEGGIE : pizza = new VeggiePizza(); } return pizza; } } package com.shma.factory.simplefactory; import com.shma.factory.simplefactory.pizza.Pizza; import com.shma.factory.simplefactory.pizza.PizzaType; /**  *   * @author admin  *  */ public class PizzaStore { private SimplePizzaFactory simplePizzaFactory; public PizzaStore(SimplePizzaFactory simplePizzaFactory) { this.simplePizzaFactory = simplePizzaFactory; } public Pizza orderPizza(PizzaType type) { Pizza pizza = simplePizzaFactory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } } package com.shma.factory.simplefactory; import com.shma.factory.simplefactory.pizza.Pizza; import com.shma.factory.simplefactory.pizza.PizzaType; public class PizzaTestMain { public static void main(String[] args) { // SimplePizzaFactory simplePizzaFactory = new SimplePizzaFactory(); // , simplePizzaFactory PizzaStore store = new PizzaStore(simplePizzaFactory); // chess Pizza pizza = store.orderPizza(PizzaType.CHESS); System.out.println(pizza); // veggie pizza = store.orderPizza(PizzaType.VEGGIE); System.out.println(pizza); } }

2. 공장 방법 모델: 창설 대상 의 인 터 페 이 스 를 정의 하고 하위 클래스 에서 실례 화 된 구체 적 인 클래스 를 결정 한다. 공장 방법 모델 은 클래스 의 실례 화 를 하위 클래스 로 연기한다.
package com.shma.factory.factorymethod.pizza;

import java.util.ArrayList;
import java.util.List;

/**
 *             
 * @author admin
 *
 */
public abstract class Pizza {

	protected String name;
	protected String dough;
	protected String sauce;
	protected List<String> toppings = new ArrayList<String>();
 
	public void prepare() {
		System.out.println("Preparing " + name);
		System.out.println("Tossing dough...");
		System.out.println("Adding sauce...");
		System.out.println("Adding toppings: ");
		for (int i = 0; i < toppings.size(); i++) {
			System.out.println("   " + toppings.get(i));
		}
	}
  
	public void bake() {
		System.out.println("Bake for 25 minutes at 350");
	}
 
	public void cut() {
		System.out.println("Cutting the pizza into diagonal slices");
	}
  
	public void box() {
		System.out.println("Place pizza in official PizzaStore box");
	}
 
	public String getName() {
		return name;
	}

	public String toString() {
		StringBuffer display = new StringBuffer();
		display.append("---- " + name + " ----
"); display.append(dough + "
"); display.append(sauce + "
"); for (int i = 0; i < toppings.size(); i++) { display.append((String )toppings.get(i) + "
"); } return display.toString(); } } package com.shma.factory.factorymethod.pizza; /**  *  pizza  * @author admin  *  */ public class ChicagoStyleCheesePizza extends Pizza { public ChicagoStyleCheesePizza() {  name = "Chicago Style Deep Dish Cheese Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce";   toppings.add("Shredded Mozzarella Cheese"); }   public void cut() { System.out.println("Cutting the pizza into square slices"); } } package com.shma.factory.factorymethod.pizza; /**  *  pizza  * @author admin  *  */ public class ChicagoStyleClamPizza extends Pizza { public ChicagoStyleClamPizza() { name = "Chicago Style Clam Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce";   toppings.add("Shredded Mozzarella Cheese"); toppings.add("Frozen Clams from Chesapeake Bay"); }   public void cut() { System.out.println("Cutting the pizza into square slices"); } } package com.shma.factory.factorymethod.pizza; /**  *  pizza  * @author admin  *  */ public class ChicagoStylePepperoniPizza extends Pizza { public ChicagoStylePepperoniPizza() { name = "Chicago Style Pepperoni Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce";   toppings.add("Shredded Mozzarella Cheese"); toppings.add("Black Olives"); toppings.add("Spinach"); toppings.add("Eggplant"); toppings.add("Sliced Pepperoni"); }   public void cut() { System.out.println("Cutting the pizza into square slices"); } } package com.shma.factory.factorymethod.pizza; /**  *  pizza  * @author admin  *  */ public class ChicagoStyleVeggiePizza extends Pizza { public ChicagoStyleVeggiePizza() { name = "Chicago Deep Dish Veggie Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce";   toppings.add("Shredded Mozzarella Cheese"); toppings.add("Black Olives"); toppings.add("Spinach"); toppings.add("Eggplant"); }   public void cut() { System.out.println("Cutting the pizza into square slices"); } } package com.shma.factory.factorymethod.pizza; /**  *  pizza  * @author admin  *  */ public class NYStyleCheesePizza extends Pizza { public NYStyleCheesePizza() {  name = "NY Style Sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce";   toppings.add("Grated Reggiano Cheese"); } } package com.shma.factory.factorymethod.pizza; /**  *  pizza  * @author admin  *  */ public class NYStyleClamPizza extends Pizza { public NYStyleClamPizza() { name = "NY Style Clam Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce";   toppings.add("Grated Reggiano Cheese"); toppings.add("Fresh Clams from Long Island Sound"); } } package com.shma.factory.factorymethod.pizza; /**  *  pizza  * @author admin  *  */ public class NYStylePepperoniPizza extends Pizza { public NYStylePepperoniPizza() { name = "NY Style Pepperoni Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce";   toppings.add("Grated Reggiano Cheese"); toppings.add("Sliced Pepperoni"); toppings.add("Garlic"); toppings.add("Onion"); toppings.add("Mushrooms"); toppings.add("Red Pepper"); } } package com.shma.factory.factorymethod.pizza; /**  *  pizza  * @author admin  *  */ public class NYStyleVeggiePizza extends Pizza { public NYStyleVeggiePizza() { name = "NY Style Veggie Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce";   toppings.add("Grated Reggiano Cheese"); toppings.add("Garlic"); toppings.add("Onion"); toppings.add("Mushrooms"); toppings.add("Red Pepper"); } }
package com.shma.factory.factorymethod.pizzastore;

import com.shma.factory.PizzaType;
import com.shma.factory.factorymethod.pizza.Pizza;

/**
 *        
 * @author admin
 *
 */
public abstract class PizzaStore {

	protected abstract Pizza createPizza(PizzaType pizzaType);
	
	public Pizza orderPizza(PizzaType pizzaType) {

		Pizza pizza = createPizza(pizzaType);
		
		System.out.println("--- Making a " + pizza.getName() + " ---");
		pizza.prepare();
		pizza.bake();
		pizza.cut();
		pizza.box();
		
		return pizza;
	} 
}

package com.shma.factory.factorymethod.pizzastore;

import com.shma.factory.PizzaType;
import com.shma.factory.factorymethod.pizza.ChicagoStyleCheesePizza;
import com.shma.factory.factorymethod.pizza.ChicagoStyleClamPizza;
import com.shma.factory.factorymethod.pizza.ChicagoStylePepperoniPizza;
import com.shma.factory.factorymethod.pizza.ChicagoStyleVeggiePizza;
import com.shma.factory.factorymethod.pizza.Pizza;

/**
 *         
 * @author admin
 *
 */
public class ChicagoPizzaStore extends PizzaStore {

	@Override
	public Pizza createPizza(PizzaType pizzaType) {
		Pizza pizza = null;
		
		switch(pizzaType) {
			case CHESS :
				pizza = new ChicagoStyleCheesePizza();
				break;
			case CLAM :
				pizza = new ChicagoStyleClamPizza();
				break;
			case PEPPERONI :
				pizza = new ChicagoStylePepperoniPizza();
				break;
			case VEGGIE :
				pizza = new ChicagoStyleVeggiePizza();
		
		}
		
		return pizza;
	}

}

package com.shma.factory.factorymethod.pizzastore;

import com.shma.factory.PizzaType;
import com.shma.factory.factorymethod.pizza.NYStyleCheesePizza;
import com.shma.factory.factorymethod.pizza.NYStyleClamPizza;
import com.shma.factory.factorymethod.pizza.NYStylePepperoniPizza;
import com.shma.factory.factorymethod.pizza.NYStyleVeggiePizza;
import com.shma.factory.factorymethod.pizza.Pizza;

/**
 *        
 * @author admin
 *
 */
public class NYPizzaStore extends PizzaStore {

	@Override
	public Pizza createPizza(PizzaType pizzaType) {
		Pizza pizza = null;
		
		switch(pizzaType) {
			case CHESS :
				pizza = new NYStyleCheesePizza();
				break;
			case CLAM :
				pizza = new NYStyleClamPizza();
				break;
			case PEPPERONI :
				pizza = new NYStylePepperoniPizza();
				break;
			case VEGGIE :
				pizza = new NYStyleVeggiePizza();
		
		}
		
		return pizza;
	}

}

package com.shma.factory;

/**
 *     
 * @author admin
 *
 */
public enum PizzaType {

	CHESS, CLAM, PEPPERONI, VEGGIE
}
package com.shma.factory.factorymethod;

import com.shma.factory.PizzaType;
import com.shma.factory.factorymethod.pizza.Pizza;
import com.shma.factory.factorymethod.pizzastore.ChicagoPizzaStore;
import com.shma.factory.factorymethod.pizzastore.NYPizzaStore;
import com.shma.factory.factorymethod.pizzastore.PizzaStore;

public class PizzaTestMain {

	public static void main(String[] args) {
		//        
		PizzaStore nyStore = new NYPizzaStore();
		//        
		PizzaStore chicagoStore = new ChicagoPizzaStore();
		
		//              
		Pizza pizza = nyStore.orderPizza(PizzaType.CHESS);
		System.out.println(pizza);
		
		//               
		chicagoStore.orderPizza(PizzaType.CHESS);
		System.out.println(pizza);
		
		pizza = nyStore.orderPizza(PizzaType.CLAM);
		System.out.println(pizza);
		
		chicagoStore.orderPizza(PizzaType.CLAM);
		System.out.println(pizza);
		
		pizza = nyStore.orderPizza(PizzaType.CLAM);
		System.out.println(pizza);
		
		chicagoStore.orderPizza(PizzaType.PEPPERONI);
		System.out.println(PizzaType.PEPPERONI);
	}
}

3. 추상 적 인 공장 모델: 인 터 페 이 스 를 제공 하여 관련 되 거나 의존 대상 의 가족 을 만 드 는 데 사용 하고 구체 적 인 유형 을 명확 하 게 지정 하지 않 아 도 된다.
package com.shma.factory.abstractfactory.ingredient;

/**
 * chessse     
 * @author admin
 *
 */
public interface Cheese {
	public String toString();
}

package com.shma.factory.abstractfactory.ingredient;

/**
 * Clams     
 * @author admin
 *
 */
public interface Clams {
	public String toString();
}

package com.shma.factory.abstractfactory.ingredient;

public interface Sauce {
	public String toString();
}

package com.shma.factory.abstractfactory.ingredient;

/**
 * Dough     
 * @author admin
 *
 */
public interface Dough {
	public String toString();
}

package com.shma.factory.abstractfactory.ingredient;

public interface Pepperoni {
	public String toString();
}

package com.shma.factory.abstractfactory.ingredient;

/**
 * Veggies     
 * @author admin
 *
 */
public interface Veggies {
	public String toString();
}

package com.shma.factory.abstractfactory.ingredient;

public class BlackOlives implements Veggies {

	public String toString() {
		return "Black Olives";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class Eggplant implements Veggies {

	public String toString() {
		return "Eggplant";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class FreshClams implements Clams {

	public String toString() {
		return "Fresh Clams from Long Island Sound";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class FrozenClams implements Clams {

	public String toString() {
		return "Frozen Clams from Chesapeake Bay";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class Garlic implements Veggies {

	public String toString() {
		return "Garlic";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class MarinaraSauce implements Sauce {
	public String toString() {
		return "Marinara Sauce";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class MozzarellaCheese implements Cheese {

	public String toString() {
		return "Shredded Mozzarella";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class Mushroom implements Veggies {

	public String toString() {
		return "Mushrooms";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class Onion implements Veggies {

	public String toString() {
		return "Onion";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class ParmesanCheese implements Cheese {

	public String toString() {
		return "Shredded Parmesan";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class PlumTomatoSauce implements Sauce {
	public String toString() {
		return "Tomato sauce with plum tomatoes";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class RedPepper implements Veggies {

	public String toString() {
		return "Red Pepper";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class ReggianoCheese implements Cheese {

	public String toString() {
		return "Reggiano Cheese";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class SlicedPepperoni implements Pepperoni {

	public String toString() {
		return "Sliced Pepperoni";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class Spinach implements Veggies {

	public String toString() {
		return "Spinach";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class ThickCrustDough implements Dough {
	public String toString() {
		return "ThickCrust style extra thick crust dough";
	}
}

package com.shma.factory.abstractfactory.ingredient;

public class ThinCrustDough implements Dough {
	public String toString() {
		return "Thin Crust Dough";
	}
}
package com.shma.factory.abstractfactory.pizza;

import com.shma.factory.abstractfactory.ingredient.Cheese;
import com.shma.factory.abstractfactory.ingredient.Clams;
import com.shma.factory.abstractfactory.ingredient.Dough;
import com.shma.factory.abstractfactory.ingredient.Pepperoni;
import com.shma.factory.abstractfactory.ingredient.Sauce;
import com.shma.factory.abstractfactory.ingredient.Veggies;

/**
 *             
 * @author admin
 *
 */
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();
  
	public void bake() {
		System.out.println("Bake for 25 minutes at 350");
	}
 
	public void cut() {
		System.out.println("Cutting the pizza into diagonal slices");
	}
  
	public void box() {
		System.out.println("Place pizza in official PizzaStore box");
	}
 
	public String getName() {
		return name;
	}

	public String toString() {
		StringBuffer result = new StringBuffer();
		result.append("---- " + name + " ----
"); if (dough != null) { result.append(dough); result.append("
"); } if (sauce != null) { result.append(sauce); result.append("
"); } if (cheese != null) { result.append(cheese); result.append("
"); } if (veggies != null) { for (int i = 0; i < veggies.length; i++) { result.append(veggies[i]); if (i < veggies.length-1) { result.append(", "); } } result.append("
"); } if (clam != null) { result.append(clam); result.append("
"); } if (pepperoni != null) { result.append(pepperoni); result.append("
"); } return result.toString(); } public void setName(String name) { this.name = name; } } package com.shma.factory.abstractfactory.pizza; import com.shma.factory.abstractfactory.PizzaIngredientFactory; /**  *  ,  * @author admin  *  */ public class CheesePizza extends Pizza { PizzaIngredientFactory ingredientFactory;   public CheesePizza(PizzaIngredientFactory ingredientFactory) { this.ingredientFactory = ingredientFactory; }   public void prepare() { System.out.println("Preparing " + name); dough = ingredientFactory.createDough(); sauce = ingredientFactory.createSauce(); cheese = ingredientFactory.createCheese(); } } package com.shma.factory.abstractfactory.pizza; import com.shma.factory.abstractfactory.PizzaIngredientFactory; public class ClamPizza extends Pizza { PizzaIngredientFactory ingredientFactory;   public ClamPizza(PizzaIngredientFactory ingredientFactory) { this.ingredientFactory = ingredientFactory; }   public void prepare() { System.out.println("Preparing " + name); dough = ingredientFactory.createDough(); sauce = ingredientFactory.createSauce(); cheese = ingredientFactory.createCheese(); clam = ingredientFactory.createClam(); } } package com.shma.factory.abstractfactory.pizza; import com.shma.factory.abstractfactory.PizzaIngredientFactory; /**  *  ,  * @author admin  *  */ public class PepperoniPizza extends Pizza { PizzaIngredientFactory ingredientFactory;   public PepperoniPizza(PizzaIngredientFactory ingredientFactory) { this.ingredientFactory = ingredientFactory; }   public void prepare() { System.out.println("Preparing " + name); dough = ingredientFactory.createDough(); sauce = ingredientFactory.createSauce(); cheese = ingredientFactory.createCheese(); veggies = ingredientFactory.createVeggies(); pepperoni = ingredientFactory.createPepperoni(); } } package com.shma.factory.abstractfactory.pizza; import com.shma.factory.abstractfactory.PizzaIngredientFactory; /**  *  ,  * @author admin  *  */ public class VeggiePizza extends Pizza { PizzaIngredientFactory ingredientFactory;   public VeggiePizza(PizzaIngredientFactory ingredientFactory) { this.ingredientFactory = ingredientFactory; }   public void prepare() { System.out.println("Preparing " + name); dough = ingredientFactory.createDough(); sauce = ingredientFactory.createSauce(); cheese = ingredientFactory.createCheese(); veggies = ingredientFactory.createVeggies(); } }
package com.shma.factory.abstractfactory.pizzastore;

import com.shma.factory.PizzaType;
import com.shma.factory.abstractfactory.pizza.Pizza;

/**
 *        
 * @author admin
 *
 */
public abstract class PizzaStore {

	protected abstract Pizza createPizza(PizzaType pizzaType);
	
	public Pizza orderPizza(PizzaType pizzaType) {

		Pizza pizza = createPizza(pizzaType);
		
		System.out.println("--- Making a " + pizza.getName() + " ---");
		pizza.prepare();
		pizza.bake();
		pizza.cut();
		pizza.box();
		
		return pizza;
	} 
}

package com.shma.factory.abstractfactory.pizzastore;

import com.shma.factory.PizzaType;
import com.shma.factory.abstractfactory.ChicagoPizzaIngredientFactory;
import com.shma.factory.abstractfactory.PizzaIngredientFactory;
import com.shma.factory.abstractfactory.pizza.CheesePizza;
import com.shma.factory.abstractfactory.pizza.ClamPizza;
import com.shma.factory.abstractfactory.pizza.PepperoniPizza;
import com.shma.factory.abstractfactory.pizza.Pizza;
import com.shma.factory.abstractfactory.pizza.VeggiePizza;

/**
 *         
 * @author admin
 *
 */
public class ChicagoPizzaStore extends PizzaStore {

	private PizzaIngredientFactory ingredientFactory;
	
	public ChicagoPizzaStore(PizzaIngredientFactory ingredientFactory) {
		this.ingredientFactory = ingredientFactory;
	}
	
	@Override
	public Pizza createPizza(PizzaType pizzaType) {
		Pizza pizza = null;
		
		switch(pizzaType) {
			case CHESS :
				pizza = new CheesePizza(ingredientFactory);
				pizza.setName("Chicago Style Cheese Pizza");
				break;
			case CLAM :
				pizza = new VeggiePizza(ingredientFactory);
				pizza.setName("Chicago Style Veggie Pizza");
				break;
			case PEPPERONI :
				pizza = new ClamPizza(ingredientFactory);
				pizza.setName("Chicago Style Clam Pizza");
				break;
			case VEGGIE :
				pizza = new PepperoniPizza(ingredientFactory);
				pizza.setName("Chicago Style Pepperoni Pizza");
				break;
		
		}
		
		return pizza;
	}

}

package com.shma.factory.abstractfactory.pizzastore;

import com.shma.factory.PizzaType;
import com.shma.factory.abstractfactory.NYPizzaIngredientFactory;
import com.shma.factory.abstractfactory.PizzaIngredientFactory;
import com.shma.factory.abstractfactory.pizza.CheesePizza;
import com.shma.factory.abstractfactory.pizza.ClamPizza;
import com.shma.factory.abstractfactory.pizza.PepperoniPizza;
import com.shma.factory.abstractfactory.pizza.Pizza;
import com.shma.factory.abstractfactory.pizza.VeggiePizza;

/**
 *        
 * @author admin
 *
 */
public class NYPizzaStore extends PizzaStore {

	private PizzaIngredientFactory ingredientFactory;
	
	public NYPizzaStore(PizzaIngredientFactory ingredientFactory) {
		this.ingredientFactory = ingredientFactory;
	}
	
	@Override
	public Pizza createPizza(PizzaType pizzaType) {
		Pizza pizza = null;
		
		switch(pizzaType) {
			case CHESS :
				pizza = new CheesePizza(ingredientFactory);
				pizza.setName("New York Style Cheese Pizza");
				break;
			case CLAM :
				pizza = new ClamPizza(ingredientFactory);
				pizza.setName("New York Style Clam Pizza");
				break;
			case PEPPERONI :
				pizza = new PepperoniPizza(ingredientFactory);
				pizza.setName("New York Style Pepperoni Pizza");
				break;
			case VEGGIE :
				pizza = new VeggiePizza(ingredientFactory);
				pizza.setName("New York Style Veggie Pizza");
				break;
		
		}
		
		return pizza;
	}

}
package com.shma.factory.abstractfactory;

import com.shma.factory.abstractfactory.ingredient.Cheese;
import com.shma.factory.abstractfactory.ingredient.Clams;
import com.shma.factory.abstractfactory.ingredient.Dough;
import com.shma.factory.abstractfactory.ingredient.Pepperoni;
import com.shma.factory.abstractfactory.ingredient.Sauce;
import com.shma.factory.abstractfactory.ingredient.Veggies;

/**
 *          ,              
 * @author admin
 *
 */
public interface PizzaIngredientFactory {
	
	public Dough createDough();
	public Sauce createSauce();
	public Cheese createCheese();
	public Veggies[] createVeggies();
	public Pepperoni createPepperoni();
	public Clams createClam();
	
}

package com.shma.factory.abstractfactory;

import com.shma.factory.abstractfactory.ingredient.BlackOlives;
import com.shma.factory.abstractfactory.ingredient.Cheese;
import com.shma.factory.abstractfactory.ingredient.Clams;
import com.shma.factory.abstractfactory.ingredient.Dough;
import com.shma.factory.abstractfactory.ingredient.Eggplant;
import com.shma.factory.abstractfactory.ingredient.FrozenClams;
import com.shma.factory.abstractfactory.ingredient.MozzarellaCheese;
import com.shma.factory.abstractfactory.ingredient.Pepperoni;
import com.shma.factory.abstractfactory.ingredient.PlumTomatoSauce;
import com.shma.factory.abstractfactory.ingredient.Sauce;
import com.shma.factory.abstractfactory.ingredient.SlicedPepperoni;
import com.shma.factory.abstractfactory.ingredient.Spinach;
import com.shma.factory.abstractfactory.ingredient.ThickCrustDough;
import com.shma.factory.abstractfactory.ingredient.Veggies;

/**
 *                 
 * @author admin
 *
 */
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 createClam() {
		return new FrozenClams();
	}

}

package com.shma.factory.abstractfactory;

import com.shma.factory.abstractfactory.ingredient.Cheese;
import com.shma.factory.abstractfactory.ingredient.Clams;
import com.shma.factory.abstractfactory.ingredient.Dough;
import com.shma.factory.abstractfactory.ingredient.FreshClams;
import com.shma.factory.abstractfactory.ingredient.Garlic;
import com.shma.factory.abstractfactory.ingredient.MarinaraSauce;
import com.shma.factory.abstractfactory.ingredient.Mushroom;
import com.shma.factory.abstractfactory.ingredient.Onion;
import com.shma.factory.abstractfactory.ingredient.Pepperoni;
import com.shma.factory.abstractfactory.ingredient.RedPepper;
import com.shma.factory.abstractfactory.ingredient.ReggianoCheese;
import com.shma.factory.abstractfactory.ingredient.Sauce;
import com.shma.factory.abstractfactory.ingredient.SlicedPepperoni;
import com.shma.factory.abstractfactory.ingredient.ThinCrustDough;
import com.shma.factory.abstractfactory.ingredient.Veggies;

/**
 *                
 * @author admin
 *
 */
public class 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 Garlic(), new Onion(), new Mushroom(), new RedPepper() };
		return veggies;
	}

	@Override
	public Pepperoni createPepperoni() {
		return new SlicedPepperoni();
	}

	@Override
	public Clams createClam() {
		return new FreshClams();
	}

}
package com.shma.factory.abstractfactory;

import com.shma.factory.PizzaType;
import com.shma.factory.abstractfactory.pizza.Pizza;
import com.shma.factory.abstractfactory.pizzastore.ChicagoPizzaStore;
import com.shma.factory.abstractfactory.pizzastore.NYPizzaStore;
import com.shma.factory.abstractfactory.pizzastore.PizzaStore;

public class PizzaTestMain {

	public static void main(String[] args) {
		
		//      
		PizzaIngredientFactory nyPizzaIngredientFactory = new NYPizzaIngredientFactory();
		PizzaIngredientFactory chPizzaIngredientFactory = new ChicagoPizzaIngredientFactory();
		
		//      
		PizzaStore pizzaStore = new NYPizzaStore(nyPizzaIngredientFactory);
		PizzaStore chPizzaStore = new ChicagoPizzaStore(chPizzaIngredientFactory);
		
		Pizza pizza = pizzaStore.orderPizza(PizzaType.CHESS);
		System.out.println(pizza);
		
		pizza = pizzaStore.orderPizza(PizzaType.PEPPERONI);
		System.out.println(pizza);
		
		pizza = chPizzaStore.orderPizza(PizzaType.PEPPERONI);
		System.out.println(pizza);
		
		pizza = chPizzaStore.orderPizza(PizzaType.CLAM);
		System.out.println(pizza);
	}
}

좋은 웹페이지 즐겨찾기