当前位置:网站首页>[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
边栏推荐
- Qt界面优化:Qt去边框与窗体圆角化
- Branch statement of process control
- DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- 【NLP】HMM隐马尔可夫+维特比分词
- Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
- AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
- 1N5408-ASEMI整流二极管1N5408
- 51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
- JumpServer
猜你喜欢
UML项目实例——抖音的UML图描述
qt之.pro文件详解
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
MDS55-16-ASEMI整流模块MDS55-16
SVN详细使用教程
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
8.2 文本预处理
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
流程控制之分支语句
随机推荐
Use of ansible and common modules
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
【NLP】HMM隐马尔可夫+维特比分词
8.3 语言模型与数据集
SVN详细使用教程
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
LotusDB 设计与实现—1 基本概念
LLVM - 生成加法
QT actual combat: Yunxi calendar
初始c语言大致框架适合复习和初步认识
JumpServer
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
Outsourcing for four years, abandoned
qt之.pro文件详解
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
抑郁症治疗的进展
UML项目实例——抖音的UML图描述
Matrix exchange row and column
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers