Factory Pattern

简单工厂模式

将变化的部分封装起来

 //简单工厂
class SimpleProductFactory{
Product createProduct(String type){
//选择--varied
if (type.equals("type1")){
product = new ...
}else if(...){
...
}else{
...
}
return product;
}
} Product orderProduct(String type){ SimpleProductFactory factory = new SimpleProductFactory(); Product product = factory.createProduct(type); //other operations... } //进一步封装
public class ProductStore{
SimpleProductFactory factory;
public ProductStore(SimpleProductFactory factory){
this.factory = factory;
} public Product orderProduct(String type){
Product product = factory.createProduct(type);
//other operations... }
} /*
这一步:
使用了一个Store类,包含了生产产品的方法。
factory在构造函数的时候传进来。
推迟了new操作。
*/

工厂方法模式

It defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclass.

 //工厂方法模式
public abstract class ProductStore{
public Product orderProduct(String type){
Product product = createProduct(type);
//product.test();
//other methods...
return product;
} protected abstract Product createProduct; //->工厂方法
//other methods...
}

抽象工厂模式

Abstract Factory creates a family of concrete objects together needed by the client.

 //抽象工厂模式

 //原料工厂
public interface ProductIngredientFactory{ public Ingredient1 createIngredient1();
public Ingredient2 createIngredient2();
public Ingredient3 createIngredient3();
//...
}
//具体的原料工厂类
//实现每一种对应的ingredient public class Product1 extends Product{
ProductIngredientFactory ingredientFactory;
public Product1(ProductIngredientFactory ingredientFactory){
this.ingredientFactory = ingredientFactory;
}
void prepare(){
ingredient1 = ingredientFactory.createIngredient1();
ingredient2 = ingredientFactory.createIngredient2();
ingredient3 = ingredientFactory.createIngredient3();
} } //重构产品商店
//每个具体的商店(继承自抽象的ProductStore)都要实现里面的工厂方法
//可能不止createProduct这一种工厂方法
//在这个工厂方法里,实现具体工厂的实例(包含了多个abstract方法),将具体工厂传入产品中 public class ProductStore1 extends ProductStore{ //implement an abstract method
protected Product createProduct(String type){
Product product = null;
ProductIngredientFactory ingredientFactory = new ProductIngredientFactory1(); //指定是原料工厂1,具体的类 if (type.equals("type1")){
product = new Product1(ingredientFactory);
}else if(...){
...
}else{
...
}
return product;
}
}

最新文章

  1. 你注意了么?int与Integer的区别
  2. css-关于absolute和relative的一些笔记
  3. 苹果Mac隐藏壁纸在哪里?Mac隐藏壁纸查找教程
  4. STORM_0006_第二个storm_topology:WordCountTopology的代码与运行
  5. 04文件与IO
  6. thinkphp验证码的实现
  7. 一个开源的可视化的jQuery工作流插件
  8. C++的函数重载 转
  9. mysql 的load data infile要使用
  10. win7下的PHP+IIS配置,找不到php5isapi.dll的问题,版本5.4.9
  11. Spring Cloud官方文档中文版-客户端负载均衡:Ribbon
  12. [sklearn]官方例程-Imputing missing values before building an estimator 随机填充缺失值
  13. 牛客网编程练习之PAT乙级(Basic Level):1032 选大王
  14. gdb不知为何显示2次析构
  15. Batch_Size 详解
  16. EAS开发环境搭建.
  17. Calendar add 方法 和set方法
  18. 数据库中DQL、DML、DDL、DCL的概念与区别
  19. abap 从屏幕输入数据
  20. Yii使用笔记 2

热门文章

  1. java 关于泛型的一些知识点
  2. Codeforces - 1189B - Number Circle - 贪心
  3. Codeforces Round #574 (Div. 2) A~E Solution
  4. 相对路径 分类: C# 2015-06-11 15:41 8人阅读 评论(0) 收藏
  5. 让 Git Bisect 帮助你
  6. 模板引擎( art-template)
  7. GeneXus笔记本—常用函数(中)
  8. python中序列类型
  9. springboot打包成jar文件无法正常运行,解决办法已经找到
  10. vue-如何输出Hello world