当前位置:网站首页>【创建型模式】工厂方法模式
【创建型模式】工厂方法模式
2022-06-11 15:18:00 【Evader1997】
回顾简单工厂模式
上回专栏介绍了简单工厂模式,它的UML类图是这样的
简单工厂模式中使用工厂类创建出加减乘除对应的四个‘产品’,此时我们创建对象是在工厂类中通过switch的case来决定。如果此时计算机需要增加一个取余的方法,我们如何做呢?
- 新建一个取余运算类继承父级运算类Operation
- 在工厂类OperationFactory的switch中新增一个case语句(需要修改这个类的代码)
新建个取余类不会影响什么,但是修改OperationFactory类中的代码就违反了开闭原则,并且每增加一个新算法类都需要修改工厂类中的代码!所以有了工厂方法!
我理解的工厂方法
先看下工厂方法的UML类图
对比两图,可以清晰的看到工厂方法将一个工厂变成了四个工厂,每个工厂都很专一,FactoryAdd工厂只生产加法对象,FactoryDiv工厂只生产除法对象,FactoryMul只生产乘法对象,FactorySub只生产减法对象。
工厂方法是简单工厂的延伸,通过回顾可以看到简单工厂的不足就在于不利于扩展。工厂方法完美解决了这一缺点,它将简单工厂模式中的工厂进一步抽象,抽成了万能工厂,这个万能工厂只提供生产对象的方法,把创建具体对象的权限交给了实现类。
工厂方法模式实现计算器
工厂方法接口
public interface FactoryMethod {
Operation createOperation();
}
实现工厂方法的工厂类
public class FactoryAdd implements FactoryMethod {
@Override
public Operation createOperation() {
return new OperationAdd();
}
}
public class FactoryDiv implements FactoryMethod {
@Override
public Operation createOperation() {
return new OperationDiv();
}
}
public class FactoryMul implements FactoryMethod {
@Override
public Operation createOperation() {
return new OperationMul();
}
}
public class FactorySub implements FactoryMethod {
@Override
public Operation createOperation() {
return new OperationSub();
}
}
测试
public class MainTest {
public static void main(String[] args) {
FactoryMethod factoryMethod = new FactorySub();
Operation operation = factoryMethod.createOperation();
operation.setNum1(2);
operation.setNum2(3);
System.out.println("计算结果是:" + operation.getResult());
}
}
运行结果:
计算结果是:-1
总结
工厂方法模式用到了多态,所以保留了简单工厂优点,客服了它的缺点,工厂方法模式下新增一个取余的功能,不需要修改已有的代码,只需要增加一个生产取余对象的工厂类即可,但是这回增加一些开发量(简单工厂多几行代码,工厂方法需要加一个类)。
如果不知道为什么使用简单工厂的同学请看专栏内的简单工厂文章,硬核分析!
简单工厂模式硬核分析
边栏推荐
- Station B executives interpret the financial report: the epidemic has no impact on the company's long-term development, and the video trend is irresistible
- Raspberry pie obtains the function of network installation system without the help of other devices
- Backtracking / activity scheduling maximum compatible activities
- Lick the dog till the last one has nothing (linear DP)
- 以 Log4j 为例,如何评估和划分安全风险
- Qualcomm WLAN framework learning (29) -- 6GHz overview
- Illustration of tiger international quarterly report: revenue of USD 52.63 million continued to be internationalized
- 详解 Kubernetes 包管理工具 Helm
- 河北 黄金寨景区新增“AED自动除颤器”保障游客生命安全!
- See from the minute, carve on the details: Exploration of SVG generated vector format website icon (favicon)
猜你喜欢

老虎国际季报图解:营收5263万美元 持续国际化布局

Learnopongl notes (IV) - Advanced OpenGL II

High number_ Chapter 6 infinite series__ Marklaurin series

05 _ 深入浅出索引(下)

Raspberry pie obtains the function of network installation system without the help of other devices

思科瑞递交科创板注册:拟募资6亿 年营收2.22亿

数据库优化

高数_第6章无穷级数__马克劳林级数

Elk log analysis system

3年亏损136亿,上市能救活威马吗?
随机推荐
Dynamically set the layoutinflater of the layout
07 _ 行锁功过:怎么减少行锁对性能的影响?
2021 年 CNCF 调查:Kubernetes 跨越鸿沟的一年
PowerShell chief architect: I used my spare time to develop projects, but I was demoted by Microsoft because of my excellent performance
High number_ Chapter 6 infinite series__ Marklaurin series
19. Insertion et suppression d'un arbre de recherche binaire
[verification of SystemVerilog] ~ test platform, hardware design description, excitation generator, monitor and comparator
06 _ 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
19. 二叉搜索树的插入删除修剪
01Tekton 初探
Arthas practice documentation
Avenue to simplicity | how to configure self attention for vit is the most reasonable?
[multi thread performance tuning] what operations cause context switching?
LoveLive! Published an AI paper: generating models to write music scores automatically
Zhejiang University has developed a UAV, which can automatically avoid obstacles and walk through the woods like a bird. The real swarm is coming
简单的C语言版本通讯录
In depth interpretation: distributed system resilience architecture ballast openchaos
老虎国际季报图解:营收5263万美元 持续国际化布局
Repository Manager之Nexus配置yum仓库
CNCF survey in 2021: a year for kubernetes to cross the gap