当前位置:网站首页>[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
边栏推荐
- 自动化的艺术
- Want to be an architect? Tamping the foundation is the most important
- Find daffodils - for loop practice
- vscode中文插件不生效问题解决
- 555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
- JumpServer
- setcontext getcontext makecontext swapcontext
- TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别
- Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
- 电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
猜你喜欢

C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【2】

51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)

机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏

C语言知识点精细详解——数据类型和变量【1】——进位计数制

循环队列的基本操作(实验)

【无标题】

Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34

线程同步、生命周期

MySQL报错packet out of order

51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
随机推荐
外包干了四年,废了...
Find daffodils - for loop practice
Some little records~
循环队列的基本操作,你学会了吗?
Use of ansible and common modules
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
pnpm安装使用
1N5408-ASEMI整流二极管1N5408
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
《JVM系列》 第七章 -- 字节码执行引擎
矩阵交换行列
抑郁症治疗的进展
【NLP】HMM隐马尔可夫+维特比分词
The initial C language framework is suitable for review and preliminary understanding
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
QT actual combat: Yunxi calendar
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
Notes on Visio drawing topology
Parameter stack pressing problem of C language in structure parameter transmission