当前位置:网站首页>Abstract Factory Pattern
Abstract Factory Pattern
2022-08-03 00:12:00 【l_ethan】

Introduction: The abstract factory pattern is used for the construction of product families. The abstract factory refers to a factory pattern used when there are multiple abstract roles.The abstract factory pattern can provide an interface to the client, so that the client can create product objects in multiple product families without specifying the specific product.
Difference: Compared with the factory method pattern, the abstract factory pattern is that the factory method pattern is for one product series, while the abstract factory pattern is for multiple product series, that is, the factory method pattern is one product series and one factory class, while the abstract factory pattern is a factory class for multiple product families.In the abstract factory pattern, the client is no longer responsible for the creation of objects, but leaves this responsibility to the specific factory class, and the client is only responsible for calling objects, thus clarifying the responsibilities of each class.And when a series of interrelated products are designed into a factory class, the client's call will become very simple, and if you want to replace this series of products, you only need to replace a factory class.

Advantages:
1. The advantages of having the factory method pattern
2. It is easy to expand a product level, in line with the principle of opening and closing
Disadvantages:
1. It is very difficult to expand a product family. It does not conform to the open-closed principle. It is necessary to modify the interface class of the top-level factory, which does not conform to the open-closed principle.
Case:
#include using namespace std;class AbstractApple {public:virtual void show()=0;};class ChinaApple :public AbstractApple{public:virtual void show() {cout << "china apple" << endl;}};class UsaApple :public AbstractApple {public:virtual void show() {cout << "usa apple" << endl;}};class AbstracBanana {public:virtual void show() = 0;};class ChinaBanana :public AbstracBanana {public:virtual void show() {cout << "china banana" << endl;}};class UsaBanana :public AbstracBanana {public:virtual void show() {cout << "usa banana" << endl;}};class AbstractFactory {public:virtual AbstractApple* create_apple() = 0;virtual AbstracBanana* create_banana() = 0;};class ChinaFactory :public AbstractFactory {public:virtual AbstractApple* create_apple() {return new ChinaApple();}virtual AbstracBanana* create_banana() {return new ChinaBanana();}};class UsaFactory : public AbstractFactory {public:virtual AbstractApple* create_apple() {return new UsaApple();}virtual AbstracBanana* create_banana() {return new UsaBanana();}};int main(){AbstractFactory* af;af = new ChinaFactory;AbstracBanana* ab;ab = af->create_banana();ab->show();AbstractApple* aa;aa = af->create_apple();aa->show();return 0;} 边栏推荐
猜你喜欢
随机推荐
【DEBUG】ImportError: Unable to import required dependencies: numpy: DLL load failed: 找不到指定的模块。
【C语言进阶】--指针典题剖析
Bena's life cycle
Bee 事务注解 @Tran 使用实例
HCIP--BGP基础实验
LeetCode 2360. 图中的最长环 基环树找环+时间戳
字节内部技术图谱 惊艳级实用
[c] Detailed explanation of operators (1)
管理工具|宝藏书签收藏管理工具
汉源高科2光12电千兆导轨式网管型工业以太网交换机双光自愈保护式以太网光交换机
【3D视觉】深度摄像头与3D重建
LeetCode 2359. 找到离给定两个节点最近的节点 基环树
ICLR 2022最佳论文:基于对比消歧的偏标签学习
Do you understand the factory pattern?
无线振弦采集仪远程修改参数的方式
SQL基础练习题(mysql)
增删改查这么多年,最后栽在MySQL的架构设计上!
PLC working principle animation
从零开始配置 vim(5)——本地设置与全局设置
Ansible installation and configuration
![[C题目]力扣138. 复制带随机指针的链表](/img/f6/75f6821343944ced9f1cdec8dffe5c.png)

![[C题目]力扣142. 环形链表 II](/img/b0/1e92f0f178089fc12cf88072d28912.png)




