1 factory pattern本质上就是对对象创建进行抽象

抽象的好处是显然的,可以方便用户去获取对象。

2 使用factory pattern的时机

第一,当一个对象的创建依赖于其它很多对象的时候,即一个对象的创建背后存在很多的依赖关系,如果用户要自己去创建这个对象的话,需要首先花很多时间去理清楚这个依赖关系,很是麻烦。如果用factory pattern的话,那么用户在创建这个对象的时候,就可以不用理会这些复杂的依赖关系了。直接让factory代劳就可以了。

例子:很多情况下依赖关系比下面的例子要复杂很多。

in PHP: Suppose you have a House object, which in turn has a Kitchen and a LivingRoom object, and the LivingRoom object has a TV object inside as well.

The simplest method to achieve this is having each object create their children on their construct method, but if the properties are relatively nested, when your House fails creating you will probably spend some time trying to isolate exactly what is failing.

The alternative is to do the following (dependency injection, if you like the fancy term):

$TVObj = new TV($param1, $param2, $param3);
$LivingroomObj = new LivingRoom($TVObj, $param1, $param2);
$KitchenroomObj = new Kitchen($param1, $param2);
$HouseObj = new House($LivingroomObj, $KitchenroomObj);

Here if the process of creating a House fails there is only one place to look, but having to use this chunk every time one wants a new House is far from convenient. Enter the Factories:

class HouseFactory {
public function create() {
$TVObj = new TV($param1, $param2, $param3);
$LivingroomObj = new LivingRoom($TVObj, $param1, $param2);
$KitchenroomObj = new Kitchen($param1, $param2);
$HouseObj = new House($LivingroomObj, $KitchenroomObj); return $HouseObj;
}
} $houseFactory = new HouseFactory();
$HouseObj = $houseFactory->create();

第二,factory mode可以带来很好的测试性

???

why factory pattern and when to use factory pattern的更多相关文章

  1. 【java设计模式】【创建模式Creational Pattern】工厂方法模式Factory Method Pattern(多态性工厂模式Polymorphic Factory Pattern、虚拟构造子模式Virtual Constructor Pattern)

    public class Test { public static void main(String[] args){ Creator ca=new ConcreteCreatorA(); ca.cr ...

  2. "Simple Factory" vs "Factory Method" vs "Abstract Factory" vs "Reflect"

    ref: http://www.cnblogs.com/zhangchenliang/p/3700820.html 1. "Simple Factory" package torv ...

  3. 【java设计模式】【行为模式Behavioral Pattern】模板方法模式Template Method Pattern

    package com.tn.pattern; public class Client { public static void main(String[] args) { AbstractClass ...

  4. 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)

    设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...

  5. 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

    原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...

  6. 【设计模式】抽象工厂模式 Abstract Factory Pattern

    简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...

  7. 【设计模式】简单工厂模式 Simple Factory Pattern

    简单工厂模式Simple Factory Pattern[Simple Factory Pattern]是设计模式里最简单的一个模式,又叫静态工厂模式[Static Factory Pattern], ...

  8. 抽象工厂(Abstract Factory),工厂方法(Factory Method),单例模式(Singleton Pattern)

    在谈工厂之前,先阐述一个观点:那就是在实际程序设计中,为了设计灵活的多态代码,代码中尽量不使用new去实例化一个对象,那么不使用new去实例化对象,剩下可用的方法就可以选择使用工厂方法,原型复制等去实 ...

  9. 工厂模式(Factory Pattern)

    一.工厂模式(Factory Pattern)的介绍 工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式.在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使 ...

随机推荐

  1. Linux C存取效率对照——堆、栈、常量区

    本文主要探讨堆和栈在使用中的存取效率.利用宏汇编指令分析訪存情况来进行简单推断. 实验环境及使用工具:i686,32位Ubuntu Linux.gcc (Ubuntu/Linaro 4.6.3-1ub ...

  2. easyui datagrid 批量编辑和提交数据

    easyui datagrid 行编辑和提交方,废话就不多说了,直接上代码 <div style="margin: 5px;"> <table id=" ...

  3. 10. 修改端口号【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51637017 spring boot 默认端口是8080,如果想要进行更改的话,只需要修改 ...

  4. 标准库Allocator的简易实现(二)

    自己实现Allocator并不难,其实只需要改变allocate和deallocate,来实现自己的内存分配策略.   下面是一个std::allocator的模拟实现 #ifndef ALLOCAT ...

  5. ES7新特性

    Array.prototype.includes Array.prototype.includes用法都容易和简单.它是一个替代indexOf,开发人员用来检查数组中是否存在值,indexOf是一种尴 ...

  6. 【HTML5】用脚本控制交互元素details元素的使用

    1.源码 <!. Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quo ...

  7. 编辑mac系统环境变量后保存,提示没有权限用到下面这个命令

    编辑的文件是vim /etc/paths :w !sudo tee % %代表当前编辑文件名 MAC:查看端口占用情况: lsof -i tcp: list open files lsof -i 用以 ...

  8. @Autowired @Transaction @Service同时出现。

    对于@Autowired,如果只写这些,肯定是根据类型自动装配这个没问题. @Service public class AcRoleServiceImpl implements AcRoleServi ...

  9. UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)

    iOS5中,UIViewController新添加了几个方法: - (void)addChildViewController:(UIViewController *)childController N ...

  10. atitit.bsh BeanShell 的动态脚本使用java

    atitit.bsh BeanShell 的动态脚本使用java 1.1. BeanShell是一个小巧免费的JAVA源码解释器 ,支持对象式的脚本语言特性,亦可嵌入到JAVA源代码中. 亦可嵌入到J ...