Layering & Contract Philosophy With additional indirection

Factory Method

The example code is as following:

 class CProduct //interface declaration
class CConcreteProductOne: public CProduct;
class CConcreteProductTwo: public CProduct; class CCreator
{
public: virtual CProduct* FactoryMethod()= ;
}
class CConcreteCreator: public CCreator
{
public: CProduct* FactoryMethod(){ return new CConcreteProductOne() OR new CConcreteProductTwo()};
}
class CClient
{
CCreator *pCreator = new CConreteCreator();
CProduct *pProduct = pCreator->FacotryMethod();
}

IoC = Inversion Of Control.

控制反转还有一个名字叫做依赖注入(Dependency Injection)。简称DI

DIP = Dependence-Inversion Principle ( low-level module is depended on high-level module by defining abstract interface in high-level).

Interface-Oriented Programming, not Implement-Oriented Programming

Applicability
Use the Factory Method pattern when

  • A class can't anticipate the class of objects it must create.
  • A class wants its subclasses to specify the objects it creates.
  • Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
  • The new proper/appropriate instance of the concrete product can be got at run-time from the real situation.

Participants

  • Product: defines the interface of objects the factory method creates.
  • ConcreteProduct : implements the Product interface.
  • Creator : declares the factory method, which returns an object of type Product.Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. may call the factory method to create a Product object.
  • ConcreteCreator: overrides the factory method to return an instance of a ConcreteProduct.

Collaborations

  • Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct.

Design Pattern ->Factory Method的更多相关文章

  1. Design Pattern ——Factory Method&Abstract Factory

    今天开始复习设计模式.设计模式相关的资料有很多,概念性的东西就画个图就可以了.把关注点放在例子上,设计模式还是要使用中才有感受. 从Factory Method&Abstract Factor ...

  2. design pattern factory method #Reprinted#

    引入人.工厂.和斧子的问题: (1),原始社会时,劳动社会基本没有分工,需要斧子的人(调用者)只好自己去磨一把斧子,每个人拥有自己的斧子,如果把大家的石斧改为铁斧,需要每个人都要学会磨铁斧的本领,工作 ...

  3. [Design Pattern] Factory Pattern 简单案例

    Factory Pattern , 即工厂模式,用于创建对象的场景,属于创建类的设计模式 . 下面是一个工厂模式案例. Shape 作为接口, Circle, Square, Rectangle 作为 ...

  4. Java Design Pattern(Factory,Singleton,Prototype,Proxy)

    一.Factory 设计模式: the most common pattern,create a new object ,eg. A a=new A();工厂模式的好处:工厂模式可以做到把创建对象单独 ...

  5. Design Pattern :Factory and Reflect in java

    interface page {     void Render(); } class pageA implements page {     @Override     public void Re ...

  6. [design pattern](5) Factory Method

    前言 在前面一章博主介绍了简单工厂模式(Simple Factory),接着上面的章节,今天博主就来介绍下工厂方法模式(Factory Method). 思考题 首先,让我们来思考下面的问题: 在上一 ...

  7. 打造属于你的提供者(Provider = Strategy + Factory Method) 设计模式 - Provider Pattern(提供者模式)

    打造属于你的提供者(Provider = Strategy + Factory Method)   1.1.1 摘要 在日常系统设计中,我们也许听说过提供者模式,甚至几乎每天都在使用它,在.NET F ...

  8. 设计模式-模板方法设计模式--Template Method design pattern

    /** * Abstract implementation of the {@link org.springframework.context.ApplicationContext} * interf ...

  9. 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)

    原文:乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pa ...

随机推荐

  1. IE兼容css3圆角的htc解决方法

    IE兼容css教程3圆角的htc解决方法 现在css3的border-radius属性可以很方便的实现圆角功能,对网站前台人员无疑是一件喜事,但悲剧的是IE6/7/8并不支持,让我们弃新技术不用,是不 ...

  2. 微信小程序传数组(Json字符串)到Java后端

    一:小程序端: wxml中代码: <!--index.wxml--> <view> <view> <button bindtap="onShow&q ...

  3. Redis学习笔记(4)—— Jedis入门

    一.Jedis介绍 Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如Java.C.C#.C++.php.Node.js.Go等. 在官方网站里列的一些Java客户端,有jedi ...

  4. java算法外传之靠工资多久能实现小目标...

    public static void main(String[] args) { //小目标 final int smallGoal=100_000_000; //月份 int month=1; // ...

  5. 简述wcf应用

    一.新建wcf 如下图:wcf可以简历俩种形式 1.库文件,就是一个类库文件,可以用windows服务或控制台开启. 2.服务应用程序,可以直接IIS上面发布. 二.库文件自动生成的类 接口类 usi ...

  6. java不可见字符 trim

    trim()的作用去掉前后的空格,  但是解析excel,出现一个字符串trim之后还是有”空格“ 做了一下实验,原来一些不可见的字符不一定是“空格”, trim()也去不掉, 只能自己写方法了

  7. Yii2 前台控制器访问权限控制

    class BaseController extends Controller { public function behaviors() { return [ 'access' => [ 'c ...

  8. Redis:存储对象的两种方式(序列化和json字符串)

    方式一:序列化操作   public class SerializeUtil {   /*   * 序列化   * */   public static byte[] serizlize(Object ...

  9. java源文件组成部分

    class HelloWorld{ public static void main(String[ ] args) { System.out.print("HelloWorld!!!&quo ...

  10. 2019.03.22 读书笔记 Linq中的IEnumerable与IQueryable

    Linq主要分为 Linq to object .Linq to XML .Linq to sql(现在都用EF了). 针对object和sql,微软分别设计了两套接口IEnumerable.IQue ...