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;
}
}

【设计模式】FactoryPattern工厂模式的更多相关文章

  1. 设计模式——抽象工厂模式及java实现

    设计模式--抽象工厂模式及java实现 设计模式在大型软件工程中很重要,软件工程中采用了优秀的设计模式有利于代码维护,方便日后更改和添加功能. 设计模式有很多,而且也随着时间在不断增多,其中最著名的是 ...

  2. 5. 星际争霸之php设计模式--抽象工厂模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  3. 3. 星际争霸之php设计模式--简单工厂模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  4. iOS 设计模式之工厂模式

    iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...

  5. 设计模式之工厂模式(Factory)

    设计模式的工厂模式一共有三种:简单工厂模式,工厂模式,抽象工厂模式 简单工厂模式原理:只有一个工厂类,通过传参的形式确定所创建的产品对象种类 代码如下: #include <stdio.h> ...

  6. php设计模式:工厂模式

    php设计模式:工厂模式 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类. 工厂模式实现: 工厂模式中任何创建对象的工厂类都要实现这个接口,实现接口的方法体中都要实现接口中的方法,它声明 ...

  7. 浅析JAVA设计模式之工厂模式(一)

    1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...

  8. java 设计模式之工厂模式与反射的结合

    工厂模式: /**  * @author Rollen-Holt 设计模式之 工厂模式  */   interface fruit{     public abstract void eat(); } ...

  9. C#学习之设计模式:工厂模式

    最近研究一下设计模式中工厂模式的应用,在此记录如下: 什么是工厂模式? 工厂模式属于设计模式中的创造型设计模式的一种.它的主要作用是协助我们创建对象,为创建对象提供最佳的方式.减少代码中的耦合程度,方 ...

  10. [JS设计模式]:工厂模式(3)

    简单工厂模式是由一个方法来决定到底要创建哪个类的实例, 而这些实例经常都拥有相同的接口. 这种模式主要用在所实例化的类型在编译期并不能确定, 而是在执行期决定的情况. 说的通俗点,就像公司茶水间的饮料 ...

随机推荐

  1. [BZOJ 3307]Cow Politics (LCA)

    [BZOJ 3307]Cow Politics (LCA) 题面 给出一棵N个点的树,树上每个节点都有颜色.对于每种颜色,求该颜色距离最远的两个点之间的距离.N≤200000 分析 显然对于每种颜色建 ...

  2. CSRF——跨站请求伪造

    一.CSRF是什么CSRF,全称:Corss-site request forgery,中文名称:跨站请求伪造.CSRF攻击比XSS攻击更具危险性,被安全界称为“沉睡的巨人”. 二.CSRF可以做什么 ...

  3. HTML5中canvas与SVG有什么区别

    SVG SVG 是一种使用 XML 描述 2D 图形的语言,它基于XML也就是我们可以为某个元素附加JavaScript事件处理器,如果SVG 对象的属性发生变化,那么浏览器能够自动重现图形. Can ...

  4. BZOJ 2141 排队 (CDQ分治)

    [BZOJ2141]排队 这道题和动态逆序对比较像(BZOJ-3295 没做过的同学建议先做这题),只是删除操作变成了交换.解法:交换操作可以变成删除加插入操作,那么这题就变成了 (时间,位置,值)的 ...

  5. Jquery datatable 配置与应用

    var EcommerceOrders = function() { var initPickers = function() { //init date pickers $('.date-picke ...

  6. Arrays.asList()报错java.lang.UnsupportedOperationException

    问题: 使用工具类Arrays.asList()方法把数组转换成集合时,不能使用修改集合相关的方法,比如add,remove.这个ArrayList是Arrays类自己定义的一个内部类!这个内部类没有 ...

  7. springBoot02- 配置文件读取测试

    1.照例登陆http://start.spring.io/ ,改个项目名(Artifact),然后下载导入Eclipse 2. 项目结构如下, 在pom中添加web依赖(不添加,就找不到RestCon ...

  8. Python3解leetcode Isomorphic Strings

    问题描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  9. javascript is ths best computer language

    alert('javascript is one of the best computer languages')

  10. jenkins的安装与使用

    以前用过hudson,前段时间听以前同事说,他现在搞jenkins,zookeeper...,现在的项目 也是手动的,所以我也就搞了一个jenkins.期间也遇到好多问题,主要是自己水平不够,网上的都 ...