当前位置:网站首页>【创建型模式】工厂方法模式
【创建型模式】工厂方法模式
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
总结
工厂方法模式用到了多态,所以保留了简单工厂优点,客服了它的缺点,工厂方法模式下新增一个取余的功能,不需要修改已有的代码,只需要增加一个生产取余对象的工厂类即可,但是这回增加一些开发量(简单工厂多几行代码,工厂方法需要加一个类)。
如果不知道为什么使用简单工厂的同学请看专栏内的简单工厂文章,硬核分析!
简单工厂模式硬核分析
边栏推荐
- Avenue to simplicity | how to configure self attention for vit is the most reasonable?
- Hot seek tiger, a list of eco economic models
- 当开源遇见 KPI,全球化 VS 本土化,开源的理想与现实该如何和解?
- See from the minute, carve on the details: Exploration of SVG generated vector format website icon (favicon)
- [process blocks and methods of SystemVerilog] ~ domain, always process block, initial process block, function, task, life cycle
- Social software soul withdraws its IPO application: Tencent is a major shareholder
- What is excess product power? Find the secret key of the second generation cs75plus in the year of the tiger
- How to play seek tiger, which has attracted much attention in the market?
- Devil cold rice # 037 devil shares the ways to become a big enterprise; Female anchor reward routine; Self discipline means freedom; Interpretation of simple interest and compound interest
- Flutter 3.0正式发布:稳定支持6大平台,字节跳动是主要用户
猜你喜欢

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

Summary of some classic embedded C interview questions

Backtracking / solution space tree permutation tree

Social software soul withdraws its IPO application: Tencent is a major shareholder

Art plus online school: Sketch common sitting posture test questions, these three angles must be mastered~

Analysis on the architecture of distributed systems - transaction and isolation level (multi object, multi operation) Part 2

Simple C language address book

19. insertion, deletion and pruning of binary search tree

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

Hot seek tiger, a list of eco economic models
随机推荐
回溯法/解空间树 排列树
Flower shop window (linear DP)
CNCF survey in 2021: a year for kubernetes to cross the gap
C语言简易版webserver
浅谈居家办公后的感想| 社区征文
Leetcode daily question - plus one
05 _ 深入浅出索引(下)
06 _ Global lock and table lock: Why are there so many obstacles to adding a field to a table?
uniapp开发微信小程序,从构建到上线
Zhejiang University has developed a UAV, which can automatically avoid obstacles and walk through the woods like a bird. The real swarm is coming
Summary of redis basic knowledge
B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡
以 Log4j 为例,如何评估和划分安全风险
How to batch insert 100000 pieces of data
容易让单片机程序跑飞的原因
Devil cold rice # 037 devil shares the ways to become a big enterprise; Female anchor reward routine; Self discipline means freedom; Interpretation of simple interest and compound interest
Hebei huangjinzhai scenic spot adds "AED automatic defibrillator" to ensure the life safety of tourists!
See from the minute, carve on the details: Exploration of SVG generated vector format website icon (favicon)
深度解读:分布式系统韧性架构压舱石OpenChaos
Arthas practice documentation