当前位置:网站首页>3. Abstract Factory
3. Abstract Factory
2022-06-12 10:12:00 【A man who is rubbed on the ground by math every day】
List of articles
3. Abstract factory pattern ( Frequency of use :*****)
- Factory method model
Each specific factory has only one or a set of overloaded factory methods , Only one product can be produced , It may result in a large number of factory classes in the system , It's bound to increase the overhead of the system
- Abstract factory pattern
A factory can produce a range of products ( A family of products ), It greatly reduces the number of factories
1. Concept
Product hierarchy : Product hierarchy is the inheritance structure of products
Product family : Product family refers to the production of , A group of products in a different product hierarchy

- Pattern motivation
When the specific products produced by the factory provided by the system are not a simple object , It's multiple product hierarchies 、 You can use the abstract factory pattern when you belong to different types of concrete products
Abstract factory pattern is the most abstract and general form of factory pattern

- Factory mode definition
Abstract factory pattern : Provides an interface for creating a series of related or interdependent objects , Without specifying their specific classes . Also known as tools (Kit) Pattern
The concrete factory in the abstract factory pattern is not just to create a product , It's responsible for creating a family of products
When a factory hierarchy can create all objects in a product family that belong to different product hierarchies , Abstract factory pattern is simpler than factory method pattern 、 More efficient
2. Abstract factory pattern structure

The abstract factory pattern includes the following 4 A character :
AbstractFactory( Abstract factory )
ConcreteFactory( Specific factory )
AbstractProduct( Abstract product )
ConcreteProduct( Specific products )
3. Abstract factory code implementation
- Typical abstract factory class code
public interface AbstractFactory {
public AbstractProductA createProductA(); // Factory method one
public AbstractProductB createProductB(); // Factory method two
……
}
- Typical specific factory class code
public class ConcreteFactory1 implements AbstractFactory {
// Factory method one
public AbstractProductA createProductA() {
return new ConcreteProductA1();
}
// Factory method two
public AbstractProductB createProductB() {
return new ConcreteProductB1();
}
……
}
4. Example
A software company wants to develop a set of interface skin library , Can be based on Java Desktop software for interface beautification . The user can select the skin through the menu when using , Different skin will provide different buttons with different visual effects 、 The text box 、 Interface elements such as combo box , For example, spring (Spring) Style skin will provide light green buttons 、 Text box with green border and combo box with green border , And summer (Summer) Style skin offers light blue buttons 、 A text box with a blue border and a combo box with a blue border , Its structure is shown in the following figure

The skin library needs to have good flexibility and scalability , Users can choose different skin freely , Developers can add new skins without modifying existing code .
Now we use the abstract factory pattern to design the interface skin library .

(1) Button: Button interface , Act as an abstract product
(2) SpringButton:Spring Button class , Act as a specific product
(3) SummerButton:Summer Button class , Act as a specific product
(4) TextField: Textbox interface , Act as an abstract product
(5) SpringTextField:Spring Textbox class , Act as a specific product
(6) SummerTextField:Summer Textbox class , Act as a specific product
(7) ComboBox: Combo box interface , Act as an abstract product
(8) SpringComboBox:Spring Combo box class , Act as a specific product
(9) SummerComboBox:Summer Combo box class , Act as a specific product
(10) SkinFactory: Interface skin factory interface , Act as an abstract factory
(11) SpringSkinFactory:Spring Skin factory , Act as a specific factory
(12) SummerSkinFactory:Summer Skin factory , Act as a specific factory
(13) Client: Client test class
Changing the skin only requires changing the configuration file
<?xml version="1.0"?>
<config>
<className>designpatterns.abstractfactory.SpringSkinFactory
</className>
</config>
5. Expand products
Add product family ( factory )
For adding new product families , The abstract factory pattern well supports the open close principle , Just add a specific product and a new specific factory , There is no need to make any changes to the existing code
Add new product hierarchy
For adding a new product hierarchy , All factory roles need to be modified , Including abstract factory classes , In all factory classes, we need to add methods to produce new products , Against the principle of opening and closing
6. Advantages and disadvantages
advantage
Isolated the generation of concrete classes , So that the client doesn't need to know what was created
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
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
7. Applicable environment of mode
A system should not depend on how product class instances are created 、 The details of composition and expression
There is more than one product family in the system , But use only one product family at a time
Products belonging to the same product family will be used together , This constraint must be reflected in the design of the system
Stable product grade structure , After the design is completed No new product hierarchy will be added to the system or existing product hierarchy will be deleted
Use environment
A system should not depend on how product class instances are created 、 The details of composition and expression
There is more than one product family in the system , But use only one product family at a time
Products belonging to the same product family will be used together , This constraint must be reflected in the design of the system
Stable product grade structure , After the design is completed No new product hierarchy will be added to the system or existing product hierarchy will be deleted
边栏推荐
- JVM (VI) Virtual machine bytecode execution engine (with stack execution process and bytecode instruction table)
- Enumerate all USB device codes
- MySQL VI Database lock
- True north reading notes
- [Wayland] Wayland agreement description
- How to do industry analysis
- markdown_ Picture side by side scheme
- JVM (VIII) Thread safety and lock optimization
- 远程桌面不能复制粘贴解决办法
- 7-13 underground maze exploration (adjacency table)
猜你喜欢

Combat tactics based on CEPH object storage

传输层协议 ——— TCP协议

First NFT platform in dfinity Ecology: impossible thoughts

Dynamic proxy

MYSQL的最左匹配原則的原理講解

7-13 underground maze exploration (adjacency table)

Auto. JS debugging: use the network mode of lightning simulator for debugging

np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍

Papaya Mobile has a comprehensive layout of cross-border e-commerce SaaS papaya orange. What are the opportunities for this new track?

How CEPH improves storage performance and storage stability
随机推荐
[CEGUI] window environment compilation
奇葩错误 -- 轮廓检测检测到边框、膨胀腐蚀开闭运算效果颠倒
Yarn scheduling
哈希表的线性探测法代码实现
Detailed explanation and use of redis data types: key and string types
Value investment
Code types and data structures corresponding to the five object types of redis
one
[path of system analyst] Chapter 18 security analysis and design of double disk system
在线电路仿真以及开源电子硬件设计介绍
Spark complex structure data retrieval method
Research on autojs wechat: the control IP in wechat on different versions of wechat or simulators is different.
Redis (II) Memory mapped data structure
UE4_以现成资源探索创建背景场景的方法
Storage R & D Engineer Recruitment
UE4_ Explore the method of creating background scenes with ready-made resources
一文读懂Dfinity生态中的首个NFT平台:IMPOSSIBLE THINGS
[SQLite3] memory debugging
[Wayland] Weston multi screen display
reflex