C++设计模式中的工厂模式详细介绍

2022-11-13 16:11:36 模式 工厂 详细介绍

1. 简单工厂模式

简单工厂模式(Simple Factory Pattern): 是指定义一个工厂类,工厂类中实现一个方法,此方法根据不同的参数返回不同的类,UML类图如下所示:

代码如下:

#include <iOStream>
using namespace std;
class Product
{
public:
	~Product() {}
	// 纯虚函数
	virtual void Create(string content) = 0;
};
class ProductA : public Product
{
public:
	void Create(string content) override {
		cout << "ProductA " << content << endl;
	}
};
class ProductB : public Product
{
public:
	void Create(string content) override {
		cout << "ProductB " << content << endl;
	}
};
class Factory
{
public:
	Product* CreateProduct(const type_info& ty_info) {
		if (ty_info == typeid(ProductA))
		{
			return m_pProductA = new ProductA();
		}
		else if (ty_info == typeid(ProductB))
		{
			return m_pProductB = new ProductB();
		}
		return NULL;
	}
	~Factory(){
		if(m_pProductA)
			delete m_pProductA;
		if(m_pProductB)
			delete m_pProductB;
	}
private:
	ProductA* m_pProductA;
	ProductB* m_pProductB;
};
int main()
{
	Factory factory;
	factory.CreateProduct(typeid(ProductA))->Create("A");
	factory.CreateProduct(typeid(ProductB))->Create("B");
	system("pause");
}

简单工厂模式的问题:

  • 当要创建的实例过多时,会存在过多的if语句
  • 当要创建新的实例时要修改工厂方法,这样做违背了开-闭原则(即对扩展开放,对修改关闭的原则)

2. 工厂方法模式

工厂方法模式(Factory Method Pattern): 是在简单工厂模式的基础上将工厂类修改为抽象类,具体的类实例创建交给抽象工厂的子类。UML类图如所示:

代码如下所示:

#include <iostream>
using namespace std;
class Product
{
public:
	~Product() {}
	// 纯虚函数
	virtual void Create(string content) = 0;
};
class ProductA : public Product
{
public:
	void Create(string content) override {
		cout << "ProductA " << content << endl;
	}
};
class ProductB : public Product
{
public:
	void Create(string content) override {
		cout << "ProductB " << content << endl;
	}
};
class Factory
{
public:
	virtual Product* CreateProduct() = 0;
	Product* m_pProduct;

	virtual ~Factory() {
		if (m_pProduct)
			delete m_pProduct;
	}
};
class FactoryA : public Factory
{
public:
	virtual Product* CreateProduct() override{
		return m_pProduct = new ProductA();
	}
};
class FactoryB : public Factory
{
public:
	virtual Product* CreateProduct() override {
		return m_pProduct = new ProductB();
	}
};
int main()
{
	FactoryA factroyA;
	FactoryB factroyB;
	factroyA.CreateProduct()->Create("A");
	factroyB.CreateProduct()->Create("B");
	system("pause");
}

工厂方法模式很好的避免了过多的if语句,同时也保证了开-闭原则,但是当类过多时会产生类"爆炸"的情况,所以具体选用什么模式需要根据实际需求进行取舍。

3. 抽象工厂模式

抽象工厂与工厂方法相比,抽象工厂允许生成不同的产品(即一个工厂存在多个产品)。代码如下所示:

```cpp
#include <iostream>
using namespace std;
class Product
{
public:
	~Product() {}
	// 纯虚函数
	virtual void Create(string content) = 0;
};
class ProductA : public Product
{
public:
	void Create(string content) override {
		cout << "ProductA " << content << endl;
	}
};
class ProductB : public Product
{
public:
	void Create(string content) override {
		cout << "ProductB " << content << endl;
	}
};
class Factory
{
public:
	virtual Product* CreateProductA() = 0;
	virtual Product* CreateProductB() = 0;
	Product* m_pProductA;
	Product* m_pProductB;
	virtual ~Factory() {
		if (m_pProductA)
			delete m_pProduct;
		if(m_pProductB)
			delete m_pProductB;
	}
};
class FactorySubOne : public Factory
{
public:
	virtual Product* CreateProductA() override{
		return m_pProductA = new ProductA();
	}
	virtual Product* CreateProductB() override {
		return m_pProductB = new ProductB();
	}
};
class FactorySubTwo : public Factory
{
public:
	virtual Product* CreateProductA() override{
		return m_pProductA = new ProductA();
	}
	virtual Product* CreateProductB() override {
		return m_pProductB = new ProductB();
	}
};
int main()
{
	FactorySubOne factroy_sub_one;
	FactorySubTwo factroy_sub_two;
	factroy_sub_one.CreateProductA()->Create("FactorySubOne  A");
	factroy_sub_one.CreateProductB()->Create("FactorySubOne  B");
	factroy_sub_two.CreateProductA()->Create("FactorySubTwo A");
	factroy_sub_two.CreateProductB()->Create("FactorySubTwo B");
	system("pause");
}

到此这篇关于c++设计模式中的工厂模式详细介绍的文章就介绍到这了,更多相关C++工厂模式内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章