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. SQL Server的一些小问题

    一.SQL Server远程调用失败 解决办法:在控制面板-程序和功能中卸载“Microsoft SQL Server 2012 Express LocalDB”,具体版本根据你安装的VS版本决定,我 ...

  2. centos中文语言安装

    1.查看当前使用的系统语言 #echo LANG 2.查看系统是否安装中文 #locale 如有zh_cn,表示已经安装了中文语言 3.安装中文 #yum groupinstall chinese-s ...

  3. python 版Faster Rcnn

    直接按照官网https://github.com/rbgirshick/py-faster-rcnn上的教程对faster Rcnn进行编译的时候,会发有一些层由于cudnn版本的更新,会报错如下: ...

  4. ES中TF-IDF算法

    概念 TF-IDF(term frequency–inverse document frequency)是一种用于资讯检索与资讯探勘的常用加权技术.TF-IDF是一种统计方法,用以评估一字词对于一个文 ...

  5. try catch和spring事务

  6. 剑指offer-合并两个排列的链接

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则.   public ListNode Merge(ListNode list1,ListNode ...

  7. 前端 HTML 常用标签 head标签相关内容 style标签 定义内部样式表

    styple标签 <!-- 定义内部样式表 --> <style type="text/css"></style>

  8. mysql分页优化方法

    mysql分页优化方法 今天遇到开发反应分页语句很慢,马上看一下到底是啥的分页语句 原分页语句 SELECT * FROM `tt` LIMIT , 执行这个语句需要6秒+时间 执行计划为全表扫描 在 ...

  9. [django]cbv方式

    cbv的方式 1.简单的url from django.views.generic import TemplateView path('', TemplateView.as_view(template ...

  10. [Java in NetBeans] Lesson 13. Multidimensional Arrays

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. Multidimensional Array: Array that has more than one dimension. ...