当前位置:网站首页>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 .
边栏推荐
猜你喜欢

LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree

Bingc (inheritance)

pytorch(环境、tensorboard、transforms、torchvision、dataloader)

When was the autowiredannotationbeanpostprocessor instantiated?

冒泡排序(Bubble Sort)

基于SDN的DDoS攻击缓解

Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?

【ARM】讯为rk3568开发板buildroot添加桌面应用
转帖——不要迷失在技术的海洋中

Leetcode513.找出树的左下角的值
随机推荐
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
Written before father's Day
423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)
重载和重写
ZigBee learning in simple terms Lecture 1
生命原来如此脆弱
Redis usage and memory optimization
[arm] build boa based embedded web server on nuc977
Learn cache lines and pseudo sharing of JVM slowly
新的征程
虚拟项目失败感想
Pytorch (network model training)
redis探索之布隆过滤器
售前分析
状态模式,身随心变
Life is so fragile
FindControl的源代码
基于SDN的DDoS攻击缓解
5分钟包你学会正则表达式
Daily production training report (17)