Factory Method
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的更多相关文章
- C#面向对象设计模式纵横谈——5.Factory Method 工厂方法模式(创建型模式)
动机 (Motivation) 在软件系统中,经常面临着“某个对象”的创建工作; 由于需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口. 如何应对这种变化?如何提供一种“封装机制” ...
- C#设计模式系列:工厂方法模式(Factory Method)
1. 工厂方法模式简介 1.1 定义 工厂方法模式定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法模式是以一个类的实例化延迟到其子类. Factory Method模式用于在不指定待创建 ...
- 小菜学习设计模式(三)—工厂方法(Factory Method)模式
前言 设计模式目录: 小菜学习设计模式(一)—模板方法(Template)模式 小菜学习设计模式(二)—单例(Singleton)模式 小菜学习设计模式(三)—工厂方法(Factory Method) ...
- 浅谈C++设计模式之工厂方法(Factory Method)
为什么要用设计模式?根本原因是为了代码复用,增加可维护性. 面向对象设计坚持的原则:开闭原则(Open Closed Principle,OCP).里氏代换原则(Liskov Substitution ...
- 深入浅出设计模式——工厂方法模式(Factory Method)
介绍在简单工厂模式中,我们提到,工厂方法模式是简单工厂模式的一个延伸,它属于Gof23中设计模式的创建型设计模式.它解决的仍然是软件设计中与创建对象有关的问题.它可以更好的处理客户的需求变化. 引入我 ...
- 工厂方法(factory method)
动机(Motivation) 在软件系统中,经常面临着“某个对象”的创建工作:由需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口.如何应对这种变化?如何提供一种“封装机制”来隔离出 ...
- Factory Method(工厂方法)-对象创建型模式
1.意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method使一个类的实例化延迟到其子类. 2.动机 框架使用抽象类定义和维护对象之间的关系.这些对象的创建通常也由框架 ...
- 设计模式之美:Factory Method(工厂方法)
索引 别名 意图 结构 参与者 适用性 缺点 效果 相关模式 命名约定 实现 实现方式(一):Creator 类是一个抽象类并且不提供它所声明的工厂方法的实现. 实现方式(二):Creator 类是一 ...
- IOS设计模式浅析之工厂方法模式(Factory Method)
概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口. 如何隔离出这个易变对象的变化,使得系统中“其它依赖该对象的对 ...
- Java设计模式-工厂方法模式(Factory Method)
工厂方法模式(Factory Method) 工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建.在以下的三种模式中,第一种如果传入的字符串有误,不能正确创 ...
随机推荐
- 集合求交集 & 去除列表中重复的元素
集合求交集: set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} 交集:set3 = set1 & set2 print(ste3) #结果为{4,5} 或者ste1. ...
- 一个ajax请求,接收json数据
<a id="inviterDel" onclick="delInviter(${item.inviterAddId})">删除</a> ...
- cors与jsonp
在.net中,可以在webApiConfig代码里写,也可以在web.config里配置,但都需要引入System.Web.Cors.这些都是服务器端的配置,对整个项目有效. {若只想对某个请求有效, ...
- [Android][Android Studio] Gradle项目中加入JNI生成文件(.so文件)
版权声明:本文作者:Qiujuer https://github.com/qiujuer; 转载请注明出处,盗版必究! ! ! https://blog.csdn.net/qiujuer/articl ...
- 时间序列分析工具箱——sweep
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/kMD8d5R/article/details/81977856 作者:徐瑞龙.量化分析师,R语言中文 ...
- 2017-2018-2 20165236 实验三《Java面向对象程序设计》实验报告
2017-2018-2 20165236 实验三<Java面向对象程序设计>实验报告 一.实验报告封面 课程:Java程序设计 班级:1652 姓名:郭金涛 ...
- Java 基础 面向对象之关键字内部类代码块修饰符
final final概念 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承,或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后 ...
- 解决PuTTY中文乱码
转载:http://lhdeyx.blog.163.com/blog/static/3181969720091115113716947/ 打开putty,选择 Category中的Windows--- ...
- Tensorflow检验GPU是否安装成功 及 使用GPU训练注意事项
1. 已经安装cuda但是tensorflow仍然使用cpu加速的问题 电脑上同时安装了GPU和CPU版本的TensorFlow,本来想用下面代码测试一下GPU程序,但无奈老是没有调用GPU. imp ...
- tomcat优化和JVM修改内存
Tomcat中的线程池(APR和ThreadPool) 2. 在Connector中指定使用共享线程池: <Connector executor="tomcatThreadPool&q ...