当前位置:网站首页>[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
边栏推荐
- 爬虫练习题(一)
- 单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
- Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
- 【NLP】HMM隐马尔可夫+维特比分词
- AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
- 51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
- I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
- MySQL报错packet out of order
- 外包幹了四年,廢了...
- JumpServer
猜你喜欢
QT actual combat: Yunxi chat room
flannel 原理 之 TUN模式
关于UDP接收icmp端口不可达(port unreachable)
线程同步、生命周期
【无标题】
LotusDB 设计与实现—1 基本概念
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
详解TCP的三次握手
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
随机推荐
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
LLVM - 生成for循环
A blog allows you to learn how to write markdown on vscode
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
c语言在结构体传参时参数压栈问题
一篇博客让你学会在vscode上编写markdown
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
本以为能躺着进华为,结果陆续收到京东/滴滴/爱奇艺offer的我迷茫了
Outsourcing for four years, abandoned
Basic regular expression
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
Solve the problem of SSH configuration file optimization and slow connection
Want to be an architect? Tamping the foundation is the most important
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)