当前位置:网站首页>[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程序、原理图及PCB文件、元器件清单和论文等】
- 基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
- SVN详细使用教程
- 关于在vs中使用scanf不安全的问题
- QT actual combat: Yunxi chat room
- Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
- Want to be an architect? Tamping the foundation is the most important
- 【Proteus仿真】自动量程(范围<10V)切换数字电压表
- The initial C language framework is suitable for review and preliminary understanding
- MySQL报错packet out of order
猜你喜欢
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
自动化的艺术
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
Nacos uses demo as configuration center (IV)
flannel 原理 之 TUN模式
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
ASEMI三相整流桥和单相整流桥的详细对比
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
随机推荐
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
查找水仙花数-for循环实践
asp.net使用MailMessage发送邮件的方法
Solve the problem of SSH configuration file optimization and slow connection
压缩映射定理
QT actual combat: Yunxi calendar
关于在vs中使用scanf不安全的问题
redis的五种数据类型
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
Logical volume creation and expansion
Docker篇 (五) MySQL的安装
LotusDB 设计与实现—1 基本概念
阿里研发三面,面试官一套组合拳让我当场懵逼
流程控制之分支语句
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
Usage of BC