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) 工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建.在以下的三种模式中,第一种如果传入的字符串有误,不能正确创 ...
随机推荐
- NOIP国王游戏
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- vuex是什么?怎么使用?哪种功能场景使用它?
vuex是vue框架中状态管理.在main.js引入store,注入.新建了一个目录store,...export.应用场景有:单页应用中,组件之间的状态.应用实例:音乐播放.登录状态.加入购物车等等
- sql语句优化(一)
1.查看执行时间和cpu占用时间 set statistics time on select * from dbo.Product set statistics time off 2.查看查询对I/0 ...
- Java基础知识(JAVA基本数据类型包装类)
基本数据类型的包装类 为什么需要包装类? Java并不是纯面向对象的语言.Java语言是一个面向对象的语言,但是Java的基本数据类型却不是面向对象的.但是我们在实际使用中经常需要将基本数据转化成对象 ...
- adb移动端测试
1. Android介绍 Android 是google公司主导的一个开放的手机操作系统,不过目前已经超过了手机的局限,而定位于移动设备的操作系统. Android一词的本义指“机器人”,取名原因很简 ...
- os.path的使用
os.path 1.返回当前目录 举个例子: (1)给出一个目录名称,返回绝对路径 project_path = "Exercise" path = os.path.dirname ...
- SQLite数据库管理工具(SQLiteStudio)v3.1.1
http://www.pc6.com/softview/SoftView_86552.html
- vux 使用swiper 垂直滚动文字 报错[Intervention] Ignored...
[Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example becaus ...
- 263A
#include<stdio.h> #include<stdlib.h> int main() { int i, j; int temp; int t_i, t_j; int ...
- NgDL:【第二周】NN基础
1.计算图的导数计算 正向比如说是计算代价函数值,反向就是增大多少a/b/c对J的影响,也就是导数的意义,这里讲的是求导链式法则. 2.向量化 节约大量计算时间 简直是100倍的时间,看来之前实现的那 ...