当前位置:网站首页>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
边栏推荐
- True north reading notes
- Redis (II) Memory mapped data structure
- 优质好书助成长 猿辅导携四大出版社推荐“暑期好书”
- High quality and good books help guide apes and recommend "good summer books" with the four major publishers
- Raw socket usage
- [chromium] location information kernel debugging
- How to implement Web3.0 and digital fashion?
- Research progress of DNA digital information storage
- 【云原生 | Kubernetes篇】Kubernetes 网络策略(NetworkPolicy)
- 在线电路仿真以及开源电子硬件设计介绍
猜你喜欢

001: what is a data lake?

MySQL III Configuration file & log file

Research progress of DNA digital information storage
Detailed explanation and use of redis data types: key and string types

IoT简介

CEPH performance optimization and enhancement

原始套接字使用

Implementation of fruit mall wholesale platform based on SSM

How CEPH improves storage performance and storage stability

基于SSM实现水果商城批发平台
随机推荐
[cloud native | kubernetes] kubernetes networkpolicy
5 most common CEPH failure scenarios
Explication du principe d'appariement le plus à gauche de MySQL
markdown_图片并排的方案
Li Yang, a scientific and technological innovator and CIO of the world's top 500 group: the success of digital transformation depends on people. Decision makers should always focus on "firewood"
1268_FreeRTOS任务上下文切换的实现
Web3.0与数字时尚,该如何落地?
JVM (VIII) Thread safety and lock optimization
How to do industry analysis
SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
Tp6 debugging (trace)
机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】
[chromium] location information kernel debugging
Overview of software definition storage (one article is enough)
远程桌面不能复制粘贴解决办法
003:what does AWS think is a data lake?
First NFT platform in dfinity Ecology: impossible thoughts
2021-02-12
MYSQL的最左匹配原則的原理講解