当前位置:网站首页>Factory mode
Factory mode
2022-06-29 09:22:00 【amateur12】
As a mode of creating objects , Where complex objects need to be generated , Can use factory mode
Complex objects are suitable for factory mode , And simple objects , In particular, only through new You can complete the created object , No need to use factory mode . If you use factory mode , You need to introduce a factory class , It will increase the complexity of the system
advantage :
Can make the code structure clear , Effectively encapsulate changes , Good scalability
Block specific product categories from callers ( Dimitar's law )
Reduce coupling
Simple factory model :
Factory , Abstract product , Specific products

The concrete commodity is realized by inheriting the abstract commodity of the factory
The factory has uploaded Abstract goods , Seven cattle , Ali , Tencent's three specific commodities are inherited upload Abstract commodities
Using a procedure is to call a factory's static method , For parameters switch Judge , Depending on the parameters , Instantiate different specific commodity objects , Then call the upload method of the specific commodity object , Upload
advantage :
The client only needs to know the corresponding parameters of this class
shortcoming :
Classes that can be created can only be considered in advance , If you need to add a new class , Then you need to change the factory class
Against the principle of opening and closing ( Open to expansion , Turn off for changes ), For the addition of new products , Factory class also needs to be modified
More and more specific products , There may be requirements for factory classes to create different instances based on different conditions . This judgment of condition is interlaced with the judgment of specific product type , Not conducive to maintenance and expansion
Factory method model :
Factory interface , Factory realization , Abstract product , Specific products

Factory method pattern is the further abstraction and promotion of simple factory pattern
The use process is to instantiate seven cows , Factories of Alibaba or Tencent , These factories all implement a factory interface , This interface has an upload method , For example, qiniu will rewrite the upload method , The class uploaded by qiniu cloud will be instantiated in the rewrite method , Seven cattle cloud upload
advantage :
The client only needs to know the factory class for creating specific products
Define an interface for creating objects , Let the class that implements this interface decide which class to instantiate . Class instantiation is deferred to subclasses , Using object-oriented polymorphism and the principle of Riemannian substitution , When the program is running , The subclass object will override the parent object , This makes the system easier to expand
Comply with opening and closing principle , Every specific product has a factory , For the addition of new products , Just add a factory to create new products , This factory inherits from the abstract factory , There is no need to change the original code , High maintainability
shortcoming :
Increased the number of classes , When there are thousands of types of products , You need thousands of factories
Abstract factory pattern :
Abstract factory , Specific factory , Abstract product , Specific products

Abstract factory pattern , Also known as toolbox mode
The abstract factory pattern is an upgraded version of the factory method pattern
Introduce plant hierarchy , It solves the problem that factory responsibilities are too heavy in the simple factory model
Make some related products into a product family ( A family of products with functions associated with different product hierarchies ), To produce by the same factory , This is the basic idea of abstract factory pattern . It also solves the problem that each factory in the factory method mode only produces one kind of products , It may result in a large number of factory classes in the system , The problem of increasing system overhead
advantage :
It is convenient to add different specific factories for fixed types of products . For example, production win System goods , Just create another production win The factory of the system commodity can
The product family can be constrained inside the class , Instead of introducing a new class for management
When multiple objects in a product family are designed to work together , It ensures that clients always use only objects in the same product family
It's convenient to add new product families , There is no need to modify the existing system , Comply with opening and closing principle
shortcoming :
complex , Readability is not as good as factory method patterns
The expansibility of product family is not good , If a new product needs to be added to the product family , Almost all factory classes need to be modified . So when using the abstract factory pattern , The division of product hierarchy is very important
Add new product hierarchy trouble , The original system needs to be greatly modified , You even need to modify the abstract layer code , This will obviously cause great inconvenience , Against the principle of opening and closing
The degradation of the factory model
When every concrete factory class in the abstract factory pattern creates only one product object , That is, when there is only one product hierarchy , Abstract factory pattern degenerates into factory method pattern ;
When abstract factory and concrete factory merge in factory method pattern , Provide a unified factory to create product objects , And when the factory method that creates the object is designed as a static method , Factory method mode degenerates into simple factory mode
边栏推荐
- UE4 installs the datasmith plug-in in version 4.20-23
- 微信小程序最新canvas2d手写签名
- Summary of 3DMAX jamming, white screen and rendering crash
- Record the field name dynamically modified by SetData of wechat applet
- pytorch总结—TENSOR ON GPU
- 深卷积神经网络时代的目标检测研究进展
- Handwriting Redux thunk
- Write down some written test questions
- [target detection] | indicator a probabilistic challenge for object detection
- 微信小程序项目:tab导航栏
猜你喜欢

微信小程序wx.navigateBack返回上一页携带参数

图片格式-webp

Augfpn: amélioration de l'apprentissage des caractéristiques à plusieurs échelles pour la détection des cibles

SSD improvements cfenet

Lffd: a lightweight fast face detector for edge detection

超融合架构和传统架构有什么区别?

3DMax 卡死、白屏、渲染死机问题总结

SSD改進CFENet

Detecting and counting tiny faces

Uni app gets the route URL of the current page
随机推荐
ActiveMQ message component publish subscribe redelivery message redelivery
ISO16000-9建筑产品和家具中挥发性有机物的测试
Wechat applet opens file stream
微信小程序最新canvas2d手写签名
微信小程序子组件向页面传值(父子组件间的通信)带源码
Jar package and war package
【To .NET】C#数据模型,从Entity Framework Core到LINQ
使用GPU训练kernel切换
pytorch总结学习系列-操作
Debug H5 page -vconsole
微信小程序搜索关键字高亮和ctrl+f搜索定位实现
The difference between cokkie and session
pytoch总结—TENSOR 和NUMPY相互转换
微信小程序打开文件流
Summary of IO streams
MH/T 6040航空材料烟密度试验
pytorch总结学习系列-广播机制
ThinkPHP 6 uses mongodb
ServerApp. iopub
微信小程序wx.navigateBack返回上一页携带参数