C++二十三种设计模式之抽象工厂模式

C++二十三种设计模式之抽象工厂模式

一、组成

抽象产品类 :声明产品功能公共接口。
具体产品类 :实现产品功能接口。
抽象工厂类 :声明创建一组具体产品公共接口。
具体工厂类:实现创建一组具体产品接口。

二、特点

1、抽象工厂创建具体产品接口返回抽象产品类型。

2、一个具体工厂创建一组特定风格产品。

三、目的

无需指定具体类的情况下,为一组相互依赖的对象提供创建接口。

四、缺点

1、违反开闭原则,新增产品类需要改动原有的工厂类。

2、场景限制问题,通常用于创建一组相互依赖的对象,意味着不适合于高度灵活的场景。

五、示例代码

javascript 复制代码
#include<iostream>
#include <vector>
#include <string>
#include <mutex>

using namespace std;

class Shape;//抽象产品类
class Rectangle;//具体产品类
class Circle;//具体产品类
class Color;//抽象产品类
class Red;//具体产品类
class Blue;//具体产品类
class AbstractFactory;//抽象工厂类
class ConcreteFactory;//具体工厂类
class ConcreteFactory2;//具体工厂类

class Shape {
public:
	virtual ~Shape() {}
	virtual void draw() = 0;
};

class Rectangle:public Shape {

public:
	~Rectangle() {}
	void draw() {
		cout << "Drawing a Rectangle" << endl;
	}
};

class Circle :public Shape {

public:
	~Circle() {}
	void draw() {
		cout << "Drawing a Circle" << endl;
	}
};

class Color {
public:
	virtual ~Color() {};
	virtual void fill() = 0;
};

class Red : public Color{
public:
	~Red() {}
	void fill() {
		cout << "Filling with Red Color" << endl;
	}
};

class Blue : public Color {
public:
	~Blue() {}
	void fill() {
		cout << "Filling with Blue Color" << endl;
	}
};

class AbstractFactory {
public:
	virtual ~AbstractFactory() {};
	virtual unique_ptr<Shape> createShape() = 0;
	virtual unique_ptr<Color> createColor() = 0;
};

class ConcreteFactory : public AbstractFactory {
public:
	~ConcreteFactory() {};
	unique_ptr<Shape> createShape() {
		return make_unique<Rectangle>();
	};
	unique_ptr<Color> createColor() {
		return make_unique<Red>();
	};
};

class ConcreteFactory2 : public AbstractFactory {
public:
	~ConcreteFactory2() {};
	unique_ptr<Shape> createShape() {
		return make_unique<Circle>();
	};
	unique_ptr<Color> createColor() {
		return make_unique<Blue>();
	};
};

int main() {
	AbstractFactory* factory = new ConcreteFactory();
	unique_ptr<Shape> shape = factory->createShape();
	unique_ptr<Color> color = factory->createColor();
	shape->draw();
	color->fill();

	AbstractFactory* factory2 = new ConcreteFactory();
	unique_ptr<Shape> shape2 = factory->createShape();
	unique_ptr<Color> color2 = factory->createColor();
	shape2->draw();
	color2->fill();
}
相关推荐
tong_li42 分钟前
给 C++ 后端服务接入企业微信告警:Webhook 原理、架构设计与生产级实现
c++·后端
ShineWinsu2 小时前
对于Qt:QMediaPlayer和QVideoWidget的解析
开发语言·c++·qt·视频·客户端·qmediaplayer·媒体播放器
善良勤劳勇敢而又聪明的老杨2 小时前
【AI编程系列】RAG 常用设计模式
设计模式·ai编程
打工仔折腾 AI2 小时前
C++ AI SDK(十):DataManager的数据库读写、事务与一致性设计
开发语言·数据库·c++·人工智能·后端·python·性能优化
沫璃染墨2 小时前
从零入门计算机网络系列(三十三):Linux I/O 多路复用详解——epoll 核心接口、底层原理与事件通知机制
linux·运维·服务器·c++·驱动开发·系统架构·硬件架构
Rabitebla2 小时前
【Linux】文件系统(四):目录、路径解析与 dentry 缓存
linux·数据结构·c++·笔记·学习·算法·缓存
kiracrimson2 小时前
最短路径:Dijkstra 与 Bellman-Ford、Floyd 各自的边界
数据结构·c++·算法·最短路径·c++学习
Meteors.2 小时前
Android 项目常见问题与 23 种设计模式应用对照表(含 Kotlin 示例)
android·设计模式·kotlin
wuminyu3 小时前
OpenJDK学习笔记:如何构建支持动态注册、注销和重新注册的 Native回调管理器
java·linux·c语言·jvm·c++
Escalating_xu3 小时前
【Redis C++】Hash、ZSet 与集群:从对象字段到积分排行榜
c++·redis·哈希算法