抽象工厂模式(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

最新文章

  1. C语言运算符优先级和口诀(转)
  2. 【转】PowerShell入门(序):为什么需要PowerShell?
  3. The Dataflow Model 论文
  4. Linux下gdb使用整理记录
  5. CABasicAnimation
  6. INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 错误
  7. Linux系统编程(3)——文件与IO之fcntl函数
  8. Spring+SpringMVC+MyBatis+easyUI整合优化篇(八)代码优化整理小记及个人吐槽
  9. [PGM] Bayes Network and Conditional Independence
  10. MySQL - 扩展性 1 概述:人多未必力量大
  11. 一张 JVM 相关的思维脑图(4.4M)
  12. 莫比乌斯反演学习笔记+[POI2007]Zap(洛谷P3455,BZOJ1101)
  13. UI5-文档-2.1-使用OpenUI5开发应用
  14. Xcode下载模拟器太慢?
  15. C++异步编程资料汇集贴
  16. 2457: [BeiJing2011]双端队列
  17. 专用于高并发的map类-----Map的并发处理(ConcurrentHashMap)
  18. php部分:网页中报表的打印,并用CSS样式控制打印的部分;
  19. JavaBean映射工具dozer学习
  20. RNN与情感分类问题实战-加载IMDB数据集

热门文章

  1. jQuery全能图片滚动插件
  2. vue项目性能优化(路由懒加载、gzip加速、cdn加速)
  3. mqtt server搭建和web中使用js-sdk订阅发布消息
  4. XAMPP添加二级域名
  5. 01Jenkins环境准备
  6. webstorm 打开后 一直停留在scanning files to index....,或跳出内存不够的提示框
  7. HTTP协议教程
  8. 微信小程序开发11-HTTPS网络通信(重点)
  9. Navicat 连接 Oracle数据库并,导入数据泵(.dmp)
  10. LoadRunner对移动互联网后端服务器压力测试