当前位置:网站首页>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;} 边栏推荐
猜你喜欢
随机推荐
【目标检测】YOLOv5:640与1280分辨率效果对比
Intensive reading of the Swin Transformer paper and analysis of its model structure
以赛促练-力扣第304场周赛反思(持续更新中)
Li Mu hands-on deep learning V2-BERT pre-training and code implementation
Jar包启动通过ClassPathResource获取不到文件路径问题
手把手教你干掉if else
A brief discussion on the transformation of .NET legacy applications
【流媒体】推流与拉流简介
快速构建电脑软件系统 、超好用经典的网页推荐汇总
golang刷leetcode:统计区间中的整数数目
树形结构构造示例代码
汉源高科千兆4光4电工业级网管型智能环网冗余以太网交换机防浪涌防雷导轨式安装
golang刷leetcode: 卖木头块
JumpServer open source bastion machine completes Loongson architecture compatibility certification
30天啃透这份Framework 源码手册直接面进大厂
[c] Detailed explanation of operators (1)
js how to get the browser zoom ratio
C# Monitor class
终于明白:有了线程,为什么还要有协程?
YARN资源调度系统介绍


![[c] Detailed explanation of operators (1)](/img/7d/5f2030dcae0f3af16f6e9a37ff041b.png)





