Question:Based on the previous article,what could you do if we must add an extra  function? For example,we need to numberA to the numberB;

Analysis:

we must update the class of 'Factroy' and continue to add another case in the expression  of 'switch - case' if we code based on ‘Simple Factory', and at the same time, we also must add a subclass to extend the class of Operation.Actually it does not accord with the principle of 'open - close' which means code is open for adding but closed for update. So in this example, we will introduce the pattern of 'Factory Method' as following.

What's Factory Method?

Here has an interface of Factory which can let its subclass to intantiate the object respectively according with the need of Client.

public class Operation {
 
 public double numberA = 0.0;
 
 public double numberB = 0.0;
 
 public double getResult(){
  
  double result = 0.0;
  
  return result;
 }
}

public interface IFactory {    

   public Operation getOperation();

}

public class AddFactory implements IFactory{

 @Override  public Operation getOperation() {     

      return new Add();

    }

}

public class Add extends Operation {  

   @Override  

  public double getResult() {

  double result = 0.0;  

  result = numberA + numberB;   

  return result;  

   }

}

Finally

Client:

public class Test {
 public static void main(String[] args) {
  IFactory add = new AddFactory();
  Operation oper = add.getOperation();
  oper.numberA = 22;
  oper.numberB = 11;
  double result = oper.getResult();
  System.out.println(result);
 }
}

In the Client,we still need to update the Client when the requirement is changed.

Factory Method的更多相关文章

  1. C#面向对象设计模式纵横谈——5.Factory Method 工厂方法模式(创建型模式)

    动机 (Motivation) 在软件系统中,经常面临着“某个对象”的创建工作; 由于需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口. 如何应对这种变化?如何提供一种“封装机制” ...

  2. C#设计模式系列:工厂方法模式(Factory Method)

    1. 工厂方法模式简介 1.1 定义 工厂方法模式定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法模式是以一个类的实例化延迟到其子类. Factory Method模式用于在不指定待创建 ...

  3. 小菜学习设计模式(三)—工厂方法(Factory Method)模式

    前言 设计模式目录: 小菜学习设计模式(一)—模板方法(Template)模式 小菜学习设计模式(二)—单例(Singleton)模式 小菜学习设计模式(三)—工厂方法(Factory Method) ...

  4. 浅谈C++设计模式之工厂方法(Factory Method)

    为什么要用设计模式?根本原因是为了代码复用,增加可维护性. 面向对象设计坚持的原则:开闭原则(Open Closed Principle,OCP).里氏代换原则(Liskov Substitution ...

  5. 深入浅出设计模式——工厂方法模式(Factory Method)

    介绍在简单工厂模式中,我们提到,工厂方法模式是简单工厂模式的一个延伸,它属于Gof23中设计模式的创建型设计模式.它解决的仍然是软件设计中与创建对象有关的问题.它可以更好的处理客户的需求变化. 引入我 ...

  6. 工厂方法(factory method)

    动机(Motivation) 在软件系统中,经常面临着“某个对象”的创建工作:由需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口.如何应对这种变化?如何提供一种“封装机制”来隔离出 ...

  7. Factory Method(工厂方法)-对象创建型模式

    1.意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method使一个类的实例化延迟到其子类. 2.动机 框架使用抽象类定义和维护对象之间的关系.这些对象的创建通常也由框架 ...

  8. 设计模式之美:Factory Method(工厂方法)

    索引 别名 意图 结构 参与者 适用性 缺点 效果 相关模式 命名约定 实现 实现方式(一):Creator 类是一个抽象类并且不提供它所声明的工厂方法的实现. 实现方式(二):Creator 类是一 ...

  9. IOS设计模式浅析之工厂方法模式(Factory Method)

    概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口. 如何隔离出这个易变对象的变化,使得系统中“其它依赖该对象的对 ...

  10. Java设计模式-工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) 工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建.在以下的三种模式中,第一种如果传入的字符串有误,不能正确创 ...

随机推荐

  1. iptables编写规则

    [-t 表名]:该规则所操作的哪个表,可以使用filter.nat等,如果没有指定则默认为filter -A:新增一条规则,到该规则链列表的最后一行 -I:插入一条规则,原本该位置上的规则会往后顺序移 ...

  2. python小练--使用正则表达式将json解析成dict

    练习python语法,自己实现了一个简单的解析json字符,存为dict字典对象. { "id":12, "name":"jack", &q ...

  3. AMI:加密的机器映像。卷

    (一)定义:镜像AMI (Amazon Machine Image,机器映像)是一个可以将操作系统.用户的应用程序.配置等一起打包的加密机器映像.用于启用实例的预配置服务器模板.每个 AMI 由一个操 ...

  4. Uncontrolled memory mapping in camera driver (CVE-2013-2595)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/hu3167343/article/details/34434235 /* 本文章由 莫灰灰 编写,转 ...

  5. Python3学习之路~6.8 多态

    多态性(polymorphisn)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作.简单的说,就是一句话:允许将子类类型 ...

  6. sqlserver生成表结构文档的方法

    只说原理了,具体 可以自己使用程序去生成htm或word文档. 1.首先获取所有的表 SELECT name, id From sysobjects WHERE xtype = 'u' ORDER B ...

  7. [py]py里的isinstance判断实例来源(含父类)

    Isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(). isinstance() 与 type() 区别: type() 不会认为子类是一种父类类型,不考虑继承关系. ...

  8. nginx命令行参数

    通过控制台进入nginx目录后 1. 启动nginx start nginx 或 nginx.exe 2. 重启nginx nginx -s reload 3. 停止nginx nginx -s st ...

  9. Linux下搭建redis服务器

    1.redis需要gcc的编译环境,在线安装gcc:#  yum install gcc-c++ 2.上传redis的压缩包到Linux系统,解压 3.进入源码目录,编译(注意要有makefile), ...

  10. [LeetCode] 747. Largest Number At Least Twice of Others_Easy

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...