当前位置:网站首页>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;} 边栏推荐
- UDP(用户数据报协议)
- JumpServer open source bastion machine completes Loongson architecture compatibility certification
- Flink优化及相关
- HCIP--BGP基础实验
- 【流媒体】推流与拉流简介
- 从零开始配置 vim(5)——本地设置与全局设置
- I interviewed a 985 graduate, and I will never forget the expression when answering the "performance tuning" question
- Li Mu hands-on deep learning V2-BERT pre-training and code implementation
- LeetCode 2359. 找到离给定两个节点最近的节点 基环树
- 面试官:可以谈谈乐观锁和悲观锁吗
猜你喜欢
随机推荐
管理工具|宝藏书签收藏管理工具
Summary of @Transactional transaction invocation and effective scenarios
汉源高科千兆4光4电工业级网管型智能环网冗余以太网交换机防浪涌防雷导轨式安装
包管理工具npm- node package management相关知识 、检查包更新、NPM包上传、更换镜像、npm ERR! registry error parsing json
一款免费的容器安全 SaaS 平台使用记录
PLC working principle animation
【c】操作符详解(一)
嗨!不来看一下如何骚气十足的登陆MySQL嘛?
终于明白:有了线程,为什么还要有协程?
单例模式你会几种写法?
YOLOv5+BiSeNet——同时进行目标检测和语义分割
golang 刷leetcode:Morris 遍历
微软SQL服务器被黑客入侵以窃取代理服务的带宽
包管理工具Chocolate - Win10如何安装Chocolate工具、快速上手、进阶用法
用户之声 | 我与GBase的缘分
golang刷leetcode: 在每个树行中找最大值
面试官居然问我:删库后,除了跑路还能干什么?
SQL基础练习题(mysql)
Do you understand the factory pattern?
双轴晶体中的锥形折射


![[C题目]力扣138. 复制带随机指针的链表](/img/f6/75f6821343944ced9f1cdec8dffe5c.png)




