当前位置:网站首页>【创建型模式】工厂方法模式
【创建型模式】工厂方法模式
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
总结
工厂方法模式用到了多态,所以保留了简单工厂优点,客服了它的缺点,工厂方法模式下新增一个取余的功能,不需要修改已有的代码,只需要增加一个生产取余对象的工厂类即可,但是这回增加一些开发量(简单工厂多几行代码,工厂方法需要加一个类)。
如果不知道为什么使用简单工厂的同学请看专栏内的简单工厂文章,硬核分析!
简单工厂模式硬核分析
边栏推荐
- 理邦仪器软件实习生面试
- 浅谈居家办公后的感想| 社区征文
- Art plus online school: Sketch common sitting posture test questions, these three angles must be mastered~
- Safepoint explanation and analysis of its placement ideas
- 新华三交换机系统基本配置命令
- 数据库优化
- uniapp开发微信小程序,从构建到上线
- Hashicopy之nomad应用编排方案03(运行一个job)
- Nomad application layout scheme 04 of hashicopy (scaling and updating a job)
- Nexus configuration Yum repository for repository manager
猜你喜欢

03 _ Transaction isolation: why can't I see it after you change it?

uniapp开发微信小程序,从构建到上线

浅析分布式系统之体系结构-事务与隔离级别(多对象、多操作)下篇

North China pushed Yale hard, MIT won the first place in a row, and the latest 2023qs world university ranking was released

Avenue to simplicity | how to configure self attention for vit is the most reasonable?

05 _ In simple terms index (Part 2)
![[azure application service] nodejs express + msal realizes the authentication experiment of API Application token authentication (AAD oauth2 idtoken) -- passport authenticate()](/img/11/7262211654680512dae0a9a696740e.png)
[azure application service] nodejs express + msal realizes the authentication experiment of API Application token authentication (AAD oauth2 idtoken) -- passport authenticate()

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

Illustration of tiger international quarterly report: revenue of USD 52.63 million continued to be internationalized

Database optimization
随机推荐
Mysql database optimization details
In depth analysis of "circle group" relationship system design | series of articles on "circle group" technology
19. Insertion et suppression d'un arbre de recherche binaire
06 _ Global lock and table lock: Why are there so many obstacles to adding a field to a table?
05 _ 深入浅出索引(下)
2021 go developer survey
高数_第6章无穷级数__马克劳林级数
Mysql(九)Your password has expired. To log in you must change it using a client that supports expired
[SystemVerilog interface] ~ interface
High number_ Chapter 6 infinite series__ Marklaurin series
Architectural concept exploration: Taking the development of card games as an example
Oauth2的理解
Taking log4j as an example, how to evaluate and classify security risks
Hashicopy之nomad应用编排方案01
Flutter 3.0 was officially released: it stably supports 6 platforms, and byte jitter is the main user
Database optimization
[mysql_12] MySQL data types
Leetcode daily question - plus one
基于 GateWay 和 Nacos 实现微服务架构灰度发布方案
Dynamically set the layoutinflater of the layout