当前位置:网站首页>Factory method pattern, abstract factory pattern
Factory method pattern, abstract factory pattern
2022-06-26 05:46:00 【Green water monster 12138】
What is factory mode
The factory pattern provides a method for creating objects in its parent class , Let its subclasses implement the concrete return object
UML chart
Factory method model 
Abstract factory pattern 
The contrast between the two
From the above UML In the picture we can see that , The factory method pattern defines only one product inheritance relationship , The abstract factory can define multiple product inheritance relationships . It can beautifully complete the expansion of the factory , The expansion of the product does not meet the opening and closing principle . The factory method model can well meet the expansion of factories and commodities , But when there are many factories and Commodities , It seems very complicated .
The following is from this website
Factory mode usage scenarios
- When you're writing code , If the exact class of an object and its dependencies cannot be predicted , The factory method can be used .
- The factory method separates the code that creates the product from the code that actually uses the product , So that we can extend the product and create part of the code without affecting other code . for example , If you need to add a new product to the application , You just need to develop a new creator subclass , Then rewrite its factory method .
- If you want users to extend the internal components of your software library or framework , The factory method can be used .
- Inheritance is probably the easiest way to extend the default behavior of a software library or framework . But when you use subclasses When replacing standard components , How does the framework recognize this subclass ?
- The solution is to centralize the code for constructing components in each framework into a single factory method , And allow anyone to override the method outside of inheriting the component .
- Let's see how it is implemented . Suppose you use open source UI Framework to write their own applications . You want to use round buttons in your app , But the original frame only supports rectangular buttons . You can use Round button RoundButton Subclass to inherit the standard Button Button class . however , You need to tell UI frame UIFramework Class uses a new subclass button instead of the default button .
- In order to achieve this function , You can open subclasses according to the basic framework class Round button UIUIWithRoundButtons , And rewrite it createButton Create button method . The method in the base class returns Button object , And the subclass you develop returns Circular button object . Now? , You can use it Round button UI Class substitution UI Framework class . It's that simple !

- If you want to reuse existing objects to save system resources , Instead of recreating objects every time , The factory method can be used .
When dealing with large resource intensive objects ( For example, database connection 、 File systems and network resources ) when , You will often encounter this kind of resource demand .
Let's think about ways to reuse existing objects :
First , You need to create storage space to store all the created objects .
When someone requests an object , The program will search the object pool for available objects .
… Then return it to the client code .
If no objects are available , The program creates a new object ( And add it to the object pool ). There's a lot of code ! And they must be in the same place , This ensures that duplicate code does not contaminate the program .Probably the most obvious , And the most convenient way , Is to put this code in the constructor of the object class we are trying to reuse . But by definition , The constructor always returns a new object , It cannot return an existing instance .
therefore , You need one that can create new objects , You can also reuse common methods of existing objects . This sounds very similar to the factory method .
- If the code needs to interact with multiple related products of different families , But because we can't get the information in advance , Or for the sake of future scalability , You don't want to build on specific product classes , under these circumstances , You can use abstract factories .
- The abstract factory provides you with an interface , Objects that can be used to create each product family . As long as the code creates objects through this interface , Then you don't generate products that are inconsistent with the types of products that your application has generated .
The relationship with other design patterns
- The factory method pattern is used in the early stages of many design work ( It's simpler , And it's easier to customize through subclasses ), Then evolved to use the abstract factory pattern 、 Prototype mode or builder Pattern ( More flexible but more complex ).
- builder Focus on how to generate complex objects step by step . Abstract factories are designed to produce a series of related objects . The abstract factory will immediately return the product , The builder pattern allows you to perform some additional construction steps before acquiring the product .
边栏推荐
- 423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)
- Kolla ansible deploy openstack Yoga version
- [activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
- Introduction to GUI programming to game practice (I)
- Internship May 29, 2019
- Feelings of virtual project failure
- 操作符的优先级、结合性、是否控制求值顺序【详解】
- 小小面试题之GET和POST的区别
- Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
- 最后一次飞翔
猜你喜欢

Unicloud cloud development obtains applet user openid

kolla-ansible部署openstack yoga版本

The difference between get and post in small interview questions

Learn cache lines and pseudo sharing of JVM slowly

The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
![[upsampling method opencv interpolation]](/img/6b/5e8f3c2844f0cbbbf03022e0efd5f0.png)
[upsampling method opencv interpolation]

【 langage c】 stockage des données d'analyse approfondie en mémoire

pytorch(网络模型训练)

Redis usage and memory optimization

MySQL数据库-01数据库概述
随机推荐
Henkel database custom operator '~~‘
ZigBee explain in simple terms lesson 2 hardware related and IO operation
The use of loops in SQL syntax
kolla-ansible部署openstack yoga版本
Daily production training report (16)
Old love letters
自定义WebSerivce作为代理解决SilverLight跨域调用WebService问题
LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
Unicloud cloud development obtains applet user openid
redis探索之布隆过滤器
ZigBee learning in simple terms Lecture 1
DOM文档
MySQL database-01 database overview
9 common classes
Positioning setting horizontal and vertical center (multiple methods)
cross entropy loss = log softmax + nll loss
组合模式、透明方式和安全方式
Bingc (inheritance)
Pytorch (network model training)
从新东方直播来探究下小程序音视频通话及互动直播