当前位置:网站首页>【工厂模式详解】工厂方法模式
【工厂模式详解】工厂方法模式
2022-04-23 14:33:00 【Vivien_oO0】
工厂模式
前面已经介绍了简单工厂模式
本文主要介绍
- 工厂方法模式
工厂方法模式
简单工厂模式的缺点是工厂类的职责相对过重,不易于扩展复杂的产品结构。
工厂方法模式(Fatory Method Pattern)是指定义一个创建对象的接口,但让实现这个接口的类来决定实例化哪个类,工厂方法模式让类的实例化推迟到子类中进行。让工厂方法模式中用户只需要关心所需产品对应的工厂,无须关心创建的细节,而且加入细心的产品时复合开闭原则。
工厂方法模式主要解决产品扩展的问题。在简单工厂模式中,随着产品链的丰富,如果每个课程的创建逻辑有区别,则工厂的职责会变得越来越多,有点像万能工厂 ,不便于维护。根据单一职责原则我们将职能继续拆分,专人干专事。Java课程由Java工厂创建,Python课程由Python工厂创建,对工厂本身也做一个抽象。来看代码:
ICourse代码:
public interface ICourse {
void record();//录制课程
}
JavaCourse代码:
public class JavaCourse implements ICourse {
@Override
public void record() {
System.out.println("录制java课程");
}
}
PythonCourse代码:
public class PythonCourse implements ICourse {
@Override
public void record() {
System.out.println("录制python课程");
}
}
ICourseFactory接口:
public interface ICourseFactory {
ICourse create();
}
再分别创建工厂,JavaCourseFactory
public class JavaCourseFactory implements ICourseFactory {
@Override
public ICourse create() {
return new JavaCourse();
}
}
PythonCourseFactory代码:
public class PythonCourseFactory implements ICourseFactory {
@Override
public ICourse create() {
return new PythonCourse();
}
}
测试代码如下:
public class Test {
public static void main(String[] args) {
ICourseFactory factory = new PythonCourseFactory();
ICourse course = factory.create();
course.record();
factory = new JavaCourseFactory();
course = factory.create();
course.record();
}
}
结果:
再来看一下类图:
可以对比简单共产看一下差异,连接开头已经给出。
工厂方法模式适用于以下场景:
- 创建对象需要大量重复代码
- 应用层不依赖于产品类实例如何被创建、如何被实现细节
- 一个类通过其子类指定创建那个对象
工厂方法模式的缺点:
- 类的个数容易过多,增加了复杂性
- 增加了系统抽象性和理解难度
版权声明
本文为[Vivien_oO0]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45795744/article/details/124327754
边栏推荐
猜你喜欢
L'externalisation a duré quatre ans.
Find daffodils - for loop practice
8.5 循环神经网络简洁实现
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
线程同步、生命周期
A blog allows you to learn how to write markdown on vscode
Uni app message push
51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
随机推荐
如何5分钟上手使用OCR
QT actual combat: Yunxi chat room
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
KVM learning resources
ASEMI三相整流桥和单相整流桥的详细对比
Redis cluster 原理
Notes on Visio drawing topology
JS recursion (1)
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
Web page, adaptive, proportional scaling
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
Outsourcing for four years, abandoned
tcp_diag 内核相关实现 1 调用层次
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
LLVM - 生成局部变量
QT interface optimization: QT border removal and form rounding
The initial C language framework is suitable for review and preliminary understanding