设计模式 - Abstract Factory模式(abstract factory pattern) 详细说明

简介:

Abstract Factory模式(abstract factory pattern) 详细说明


本文地址: http://blog.csdn.net/caroline_wendy/article/details/27091671


參考工厂模式: http://blog.csdn.net/caroline_wendy/article/details/27081511


抽象工厂模式: 提供一个接口, 用于创建相关或依赖对象的家族, 而不须要明白指定详细类.

所有代码: http://download.csdn.net/detail/u012515223/7403553


详细方法:

1. 提供一个抽象工厂(abstract factory)接口(interface)类, 不同的详细工厂(concrete factory)继承此类.

代码:

/**
 * @time 2014年5月26日
 */
package factory;

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

2. 详细工厂(concrete factory), 实现抽象工厂(abstract factory)接口(interface).

代码:

/**
 * @time 2014年5月26日
 */
package factory;

/**
 * @author C.L.Wang
 *
 */
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createDough()
	 */
	@Override
	public Dough createDough() {
		// TODO Auto-generated method stub
		return new ThinCrustDough();
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createSauce()
	 */
	@Override
	public Sauce createSauce() {
		// TODO Auto-generated method stub
		return new MarinaraSauce();
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createCheese()
	 */
	@Override
	public Cheese createCheese() {
		// TODO Auto-generated method stub
		return new ReggianoCheese();
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createVeggies()
	 */
	@Override
	public Veggies[] createVeggies() {
		// TODO Auto-generated method stub
		Veggies veggies[] = {new Garlic(), new Onion(), new Mushroom(), new RedPepper()};
		return veggies;
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createPepperoni()
	 */
	@Override
	public Pepperoni createPepperoni() {
		// TODO Auto-generated method stub
		return new SlicedPepperoni();
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createClam()
	 */
	@Override
	public Clams createClam() {
		// TODO Auto-generated method stub
		return new FreshClams();
	}

}


/**
 * @time 2014年5月26日
 */
package factory;

/**
 * @author C.L.Wang
 *
 */
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createDough()
	 */
	@Override
	public Dough createDough() {
		// TODO Auto-generated method stub
		return new ThickCrustDough();
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createSauce()
	 */
	@Override
	public Sauce createSauce() {
		// TODO Auto-generated method stub
		return new PlumTomatoSauce();
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createCheese()
	 */
	@Override
	public Cheese createCheese() {
		// TODO Auto-generated method stub
		return new MozzarellaCheese();
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createVeggies()
	 */
	@Override
	public Veggies[] createVeggies() {
		// TODO Auto-generated method stub
		Veggies veggies[] = {new BlackOlives(), new Spinach(), new Eggplant()};
		return veggies;
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createPepperoni()
	 */
	@Override
	public Pepperoni createPepperoni() {
		// TODO Auto-generated method stub
		return new SlicedPepperoni();
	}

	/* (non-Javadoc)
	 * @see factory.PizzaIngredientFactory#createClam()
	 */
	@Override
	public Clams createClam() {
		// TODO Auto-generated method stub
		return new FrozenClams();
	}

}

3. 产品抽象(abstract)父类, 提供接口, 供详细产品(concrete product)调用.

代码:

/**
 * @time 2014年5月26日
 */
package factory;

/**
 * @author C.L.Wang
 *
 */
public abstract class Pizza {
	String name;
	Dough dough; //生面团
	Sauce sauce; //调味汁
	Veggies veggies[];
	Cheese cheese;
	Pepperoni pepperoni;
	Clams clam;
	
	abstract void prepare();
	
	void bake() {
		System.out.println("Bake for 25 minutes at 350");
	}
	
	void cut() {
		System.out.println("Cutting the pizza into diagonal slices");
	}
	
	void box() {
		System.out.println("Place pizza in official PizzaStore box");
	}
	
	void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
}

4. 详细产品(concrete product)继承产品抽象(abstract)父类, 使用工厂类实现继承接口通过不同的工厂生产不同的产品;

代码:

/**
 * @time 2014年5月26日
 */
package factory;

/**
 * @author C.L.Wang
 *
 */
public class CheesePizza extends Pizza {

	PizzaIngredientFactory pizzaIngredientFactory;
	
	public CheesePizza(PizzaIngredientFactory pizzaIngredientFactory) {
		this.pizzaIngredientFactory = pizzaIngredientFactory;
	}
	
	/* (non-Javadoc)
	 * @see factory.Pizza#prepare()
	 */
	@Override
	void prepare() {
		// TODO Auto-generated method stub
		System.out.println("Preparing " + name);
		dough = pizzaIngredientFactory.createDough();
		sauce = pizzaIngredientFactory.createSauce();
		cheese = pizzaIngredientFactory.createCheese();
		System.out.println("Ingredient : " + dough + ", " + sauce + ", " + cheese);
	}

}


/**
 * @time 2014年5月26日
 */
package factory;

/**
 * @author C.L.Wang
 *
 */
public class ClamPizza extends Pizza {

	PizzaIngredientFactory pizzaIngredientFactory;
	
	public ClamPizza(PizzaIngredientFactory pizzaIngredientFactory) {
		this.pizzaIngredientFactory = pizzaIngredientFactory;
	}
	
	/* (non-Javadoc)
	 * @see factory.Pizza#prepare()
	 */
	@Override
	void prepare() {
		// TODO Auto-generated method stub
		System.out.println("Preparing " + name);
		dough = pizzaIngredientFactory.createDough();
		sauce = pizzaIngredientFactory.createSauce();
		cheese = pizzaIngredientFactory.createCheese();
		clam = pizzaIngredientFactory.createClam();
		System.out.println("Ingredient : " + dough + ", " 
				+ sauce + ", " + cheese + ", " + clam);
	}

}

5. 详细调用函数, 通过传递不同的參数, 调用不同的工厂, 生产出不同的产品.

代码:

/**
 * @time 2014年5月26日
 */
package factory;

/**
 * @author C.L.Wang
 *
 */
public class NYPizzaStore extends PizzaStore {

	/* (non-Javadoc)
	 * @see factory.PizzaStore#createPizza(java.lang.String)
	 */
	@Override
	Pizza createPizza(String item) {
		// TODO Auto-generated method stub
		Pizza pizza = null;
		PizzaIngredientFactory pizzaIngredientFactory = new NYPizzaIngredientFactory();
		if (item.equals("cheese")) {
			pizza = new CheesePizza(pizzaIngredientFactory);
			pizza.setName("New York Style Cheese Pizza");
		} else if (item.equals("clam")) {
			pizza = new ClamPizza(pizzaIngredientFactory);
			pizza.setName("New York Style Clam Pizza");
		}
		
		return pizza;
	}

}


/**
 * @time 2014年5月26日
 */
package factory;

/**
 * @author C.L.Wang
 *
 */
public class ChicagoPizzaStore extends PizzaStore {

	/* (non-Javadoc)
	 * @see factory.PizzaStore#createPizza(java.lang.String)
	 */
	@Override
	Pizza createPizza(String item) {
		// TODO Auto-generated method stub
		Pizza pizza = null;
		PizzaIngredientFactory pizzaIngredientFactory = new ChicagoPizzaIngredientFactory();
		if (item.equals("cheese")) {
			pizza = new CheesePizza(pizzaIngredientFactory);
			pizza.setName("Chicago Style Cheese Pizza");
		} else if (item.equals("clam")) {
			pizza = new ClamPizza(pizzaIngredientFactory);
			pizza.setName("Chicago Style Clam Pizza");
		}
		
		return pizza;
	}

}

6. 測试函数

代码:

/**
 * @time 2014年5月26日
 */
package factory;

/**
 * @author C.L.Wang
 *
 */
public class PizzaTestDrive {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PizzaStore nyStore = new NYPizzaStore();
		PizzaStore chicagoStore = new ChicagoPizzaStore();
		
		Pizza pizza = nyStore.orderPizza("cheese");
		System.out.println("Ethan ordered a " + pizza.getName() + "\n");
		
		pizza = chicagoStore.orderPizza("cheese");
		System.out.println("Joel ordered a " + pizza.getName() + "\n");
		
		pizza = nyStore.orderPizza("clam");
		System.out.println("Ethan ordered a " + pizza.getName() + "\n");
		
		pizza = chicagoStore.orderPizza("clam");
		System.out.println("Joel ordered a " + pizza.getName() + "\n");
	}

}

7. 输出:

Preparing New York Style Cheese Pizza
Ingredient : Thin Crust Dough, Marinara Sauce, Reggiano Cheese
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a New York Style Cheese Pizza

Preparing Chicago Style Cheese Pizza
Ingredient : ThickCrust style extra thick crust dough, Tomato sauce with plum tomatoes, Shredded Mozzarella
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Joel ordered a Chicago Style Cheese Pizza

Preparing New York Style Clam Pizza
Ingredient : Thin Crust Dough, Marinara Sauce, Reggiano Cheese, Fresh Clams from Long Island Sound
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a New York Style Clam Pizza

Preparing Chicago Style Clam Pizza
Ingredient : ThickCrust style extra thick crust dough, Tomato sauce with plum tomatoes,...
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Joel ordered a Chicago Style Clam Pizza








版权声明:本文博主原创文章。博客,未经同意不得转载。






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4883389.html,如需转载请自行联系原作者


相关文章
|
10天前
|
设计模式 SQL 算法
设计模式了解哪些,模版模式
设计模式了解哪些,模版模式
19 0
|
29天前
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
37 0
|
3月前
|
设计模式 存储 算法
Java 设计模式最佳实践:三、行为模式
Java 设计模式最佳实践:三、行为模式
|
3月前
|
设计模式 算法 Java
行为型设计模式-策略模式(Strategy Pattern)
行为型设计模式-策略模式(Strategy Pattern)
|
2月前
|
设计模式 前端开发 JavaScript
观察者模式 vs 发布-订阅模式:两种设计模式的对决!
欢迎来到前端入门之旅!这个专栏是为那些对Web开发感兴趣、刚刚开始学习前端的读者们打造的。无论你是初学者还是有一些基础的开发者,我们都会在这里为你提供一个系统而又亲切的学习平台。我们以问答形式更新,为大家呈现精选的前端知识点和最佳实践。通过深入浅出的解释概念,并提供实际案例和练习,让你逐步建立起一个扎实的基础。无论是HTML、CSS、JavaScript还是最新的前端框架和工具,我们都将为你提供丰富的内容和实用技巧,帮助你更好地理解并运用前端开发中的各种技术。
|
6天前
|
设计模式 Java 数据库
小谈设计模式(2)—简单工厂模式
小谈设计模式(2)—简单工厂模式
|
4天前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
12 1
|
6天前
|
设计模式 Java
小谈设计模式(9)—工厂方法模式
小谈设计模式(9)—工厂方法模式
|
1月前
|
设计模式 编译器
解析器模式--设计模式
解析器模式--设计模式
17 0
|
1月前
|
设计模式 算法
构建器模式--设计模式
构建器模式--设计模式
17 0