当前位置:网站首页>[detailed explanation of factory mode] factory method mode
[detailed explanation of factory mode] factory method mode
2022-04-23 14:35:00 【Vivien_ oO0】
List of articles
Factory mode
I've already introduced Simple factory model
This paper mainly introduces
- Factory method model
Factory method model
The disadvantage of the simple factory model is that the responsibilities of the factory class are relatively heavy , It is not easy to expand complex product structure .
Factory method model (Fatory Method Pattern) It refers to defining an interface for creating objects , But let the class that implements this interface decide which class to instantiate , The factory method pattern postpones the instantiation of a class to a subclass . In the factory method mode, users only need to care about the factory corresponding to the required product , Don't care about the details of the creation , And the principle of compound opening and closing when adding careful products .
The factory method mode mainly solves the problem of product expansion . In simple factory mode , With the enrichment of product chain , If the creation logic of each course is different , Then the responsibilities of the factory will become more and more , It's kind of like a universal factory , Not easy to maintain . According to the principle of single responsibility, we will continue to split our functions , Someone to do special work .Java The course consists of Java The factory to create ,Python The course consists of Python The factory to create , Make an abstraction of the factory itself . Look at code :
ICourse Code :
public interface ICourse {
void record();// Recording lessons
}
JavaCourse Code :
public class JavaCourse implements ICourse {
@Override
public void record() {
System.out.println(" Recording java Course ");
}
}
PythonCourse Code :
public class PythonCourse implements ICourse {
@Override
public void record() {
System.out.println(" Recording python Course ");
}
}
ICourseFactory Interface :
public interface ICourseFactory {
ICourse create();
}
Then create factories separately ,JavaCourseFactory
public class JavaCourseFactory implements ICourseFactory {
@Override
public ICourse create() {
return new JavaCourse();
}
}
PythonCourseFactory Code :
public class PythonCourseFactory implements ICourseFactory {
@Override
public ICourse create() {
return new PythonCourse();
}
}
The test code is as follows :
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();
}
}
result :
Let's look at the class diagram :
You can compare simple communism to see the difference , The beginning of the connection has given .
The factory method pattern applies to the following scenarios :
- Creating objects requires a lot of repetitive code
- The application layer does not depend on how product class instances are created 、 How to implement details
- A class specifies which object to create through its subclasses
Disadvantages of factory method pattern :
- It's easy to have too many classes , Added complexity
- It increases the abstraction and understanding difficulty of the system
版权声明
本文为[Vivien_ oO0]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231432380186.html
边栏推荐
- 51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
- 直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
- 详解TCP的三次握手
- Docker (V) MySQL installation
- 压缩映射定理
- c语言在结构体传参时参数压栈问题
- 8.2 文本预处理
- Redis cluster 原理
- Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
- 51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
猜你喜欢
A blog allows you to learn how to write markdown on vscode
flannel 原理 之 TUN模式
详解TCP的三次握手
API Gateway/API 网关(四) - Kong的使用 - 集成Jwt和熔断插件
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
循环队列的基本操作,你学会了吗?
自动化的艺术
Branch statement of process control
Detailed explanation of SAR command
Want to be an architect? Tamping the foundation is the most important
随机推荐
First acquaintance with STL
ArrayList collection basic usage
Detailed explanation of C language P2 selection branch statement
初识STL
8.3 语言模型与数据集
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
一篇博客让你学会在vscode上编写markdown
详解TCP的三次握手
L'externalisation a duré quatre ans.
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
想要成为架构师?夯实基础最重要
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
全连接层的作用是什么?
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
Outsourcing for four years, abandoned
Use of ansible and common modules
API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in
自动化的艺术
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code