当前位置:网站首页>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 (User Datagram Protocol)
Xcode13.1 run engineering error fatal error: 'IFlyMSC/IFly h' file not found
Vscode快速入门、 插件安装、插件位置、修改vscode默认引用插件的路径、在命令行总配置code、快捷键
回文自动机+CodeTON Round 2 C,D
Bena's life cycle
PyRosetta 安装方法之Conda安装
Ansible installation and configuration
golang刷leetcode:到达角落需要移除障碍物的最小数目
网络运维系列:健康检查的方式
The Orsay in Informatics (1256: Bouquet for Algernon)
千人优学 | GBase 8s数据库2022年6月大学生专场实训圆满结束
golang 刷leetcode:统计打字方案数
手把手教你干掉if else
最近火爆朋友圈的“广告电商”,核心商业模式是什么,广告收入真实靠谱吗?
JumpServer open source bastion machine completes Loongson architecture compatibility certification
解道9-编程技术6
golang 刷leetcode:祖玛游戏
X 2 Earn必须依靠旁氏启动?GameFi的出路在哪?(下)
y85.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶、pushgateway和prometheus存储(十六)
Informatics orsay a tong (1258: 【 9.2 】 digital pyramid)