当前位置:网站首页>[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
边栏推荐
- 初识STL
- AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
- 555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
- Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
- ArrayList collection basic usage
- 如何5分钟上手使用OCR
- TUN 设备原理
- [servlet] detailed explanation of servlet (use + principle)
- 抑郁症治疗的进展
- Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
猜你喜欢

Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)

51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等

ArrayList集合基本使用

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

API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance

GIS数据处理-cesium中模型位置设置

想要成为架构师?夯实基础最重要

PWM speed regulation control system of DC motor based on 51 single chip microcomputer (with complete set of data such as Proteus simulation + C program)

顺序表的操作,你真的学会了吗?

交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
随机推荐
Outsourcing for four years, abandoned
LLVM - 生成加法
ASEMI超快恢复二极管与肖特基二极管可以互换吗
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
C语言p2选择分支语句详解
Docker (V) MySQL installation
外包干了四年,废了...
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
A blog allows you to learn how to write markdown on vscode
UML项目实例——抖音的UML图描述
Qt界面优化:Qt去边框与窗体圆角化
flannel 原理 之 TUN模式
循环队列的基本操作(实验)
流程控制之分支语句
LotusDB 设计与实现—1 基本概念
Want to be an architect? Tamping the foundation is the most important
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
8.4 循环神经网络从零实现