工厂方法模式
来源:互联网 发布:一次性餐具的危害数据 编辑:程序博客网 时间:2024/05/16 04:15
工厂方法模式与简单工厂模式的对比
简单工厂模式的最大优点就是在于工厂类中包含了必要的逻辑判断,可以根据客户端的需求自动的去选择需要的类.对于客户端而言,去除了与具体产品的依赖;但是,这违背了封闭开放原则(增加新的类时需要去改动原来的工厂类)
工厂方法模式
Factory Method,定义了一个用于创建对象的接口,让子类决定实例化哪一个类.使一个类的实例化延迟到其子类
工厂方法模式结构图
工厂方法的Demo
运算类接口
public interface IOperation { int opera(int x1,int x2);}
运算类实现
public class AddOperation implements IOperation { @Override public int opera(int x1, int x2) { return x1+x2; }}
public class SubOperation implements IOperation { @Override public int opera(int x1, int x2) { return x1-x2; }}
工厂类接口
public interface IFactory { IOperation createOperation();}
工厂类实现
public class AddOpertionFactory implements IFactory { @Override public IOperation createOperation() { return new AddOperation(); }}public class SubOperationFactory implements IFactory { @Override public IOperation createOperation() { return new SubOperation(); }}
工厂方法的作用
需要还需要增加乘法的元算类,只需要实现运算类接口,在创建对应的工厂类即可实现新的功能;不需要像简单工厂那样修改原来的代码
阅读全文
0 0
- 工厂模式 -- 工厂方法
- 工厂模式-工厂方法
- 工厂模式-工厂方法
- 工厂 > 工厂方法模式
- 工厂模式-工厂方法模式
- 工厂模式-工厂方法模式
- 工厂模式---工厂方法模式
- 【模式】工厂方法模式
- 工厂方法,抽象工厂模式
- C#工厂模式-工厂方法
- 工厂模式 和工厂方法
- 工厂模式-C-工厂方法
- 工厂模式以及工厂方法
- 简单工厂、工厂方法、抽象工厂模式
- 简单工厂、工厂方法、抽象工厂模式
- 工厂模式(简单工厂、工厂方法)
- 简单工厂|工厂方法|抽象工厂模式
- 设计模式-工厂模式(简单工厂模式+工厂方法模式)
- Zynq-Linux移植学习笔记之11-qspi驱动配置与测试接口
- linux 配置mailx发送到外网邮箱
- mysql数据库备份还原
- jquery和bootstrap
- (四)BufferedOutputStream源码学习
- 工厂方法模式
- 第六篇
- flume学习(五):Flume Channel Selectors使用
- CentOS 7.0+hadoop 2.7搭建集群
- 安装sass
- 交叉编译
- linux 点命令
- 蓝桥杯 算法训练 和为T
- JavaScript Jquery