Pattern-No.06 设计模式之工厂模式

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 + " ----\n");
		display.append(dough + "\n");
		display.append(sauce + "\n");
		for (int i = 0; i < toppings.size(); i++) {
			display.append((String )toppings.get(i) + "\n");
		}
		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 + " ----\n");
		display.append(dough + "\n");
		display.append(sauce + "\n");
		for (int i = 0; i < toppings.size(); i++) {
			display.append((String )toppings.get(i) + "\n");
		}
		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 + " ----\n");
		if (dough != null) {
			result.append(dough);
			result.append("\n");
		}
		if (sauce != null) {
			result.append(sauce);
			result.append("\n");
		}
		if (cheese != null) {
			result.append(cheese);
			result.append("\n");
		}
		if (veggies != null) {
			for (int i = 0; i < veggies.length; i++) {
				result.append(veggies[i]);
				if (i < veggies.length-1) {
					result.append(", ");
				}
			}
			result.append("\n");
		}
		if (clam != null) {
			result.append(clam);
			result.append("\n");
		}
		if (pepperoni != null) {
			result.append(pepperoni);
			result.append("\n");
		}
		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);
	}
}


你可能感兴趣的:(Pattern-No.06 设计模式之工厂模式)