欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++設(shè)計模式中的工廠模式詳細介紹

 更新時間:2022年09月01日 09:51:25   作者:憤怒的小黃鴨  
工廠模式,是一種實例化對象的方式,只要輸入需要實例化對象的名字,就可以通過工廠對象的相應(yīng)工廠函數(shù)來制造你需要的對象

1. 簡單工廠模式

簡單工廠模式(Simple Factory Pattern): 是指定義一個工廠類,工廠類中實現(xiàn)一個方法,此方法根據(jù)不同的參數(shù)返回不同的類,UML類圖如下所示:

代碼如下:

#include <iostream>
using namespace std;
class Product
{
public:
	~Product() {}
	// 純虛函數(shù)
	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");
}

簡單工廠模式的問題:

  • 當要創(chuàng)建的實例過多時,會存在過多的if語句
  • 當要創(chuàng)建新的實例時要修改工廠方法,這樣做違背了開-閉原則(即對擴展開放,對修改關(guān)閉的原則)

2. 工廠方法模式

工廠方法模式(Factory Method Pattern): 是在簡單工廠模式的基礎(chǔ)上將工廠類修改為抽象類,具體的類實例創(chuàng)建交給抽象工廠的子類。UML類圖如所示:

代碼如下所示:

#include <iostream>
using namespace std;
class Product
{
public:
	~Product() {}
	// 純虛函數(shù)
	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語句,同時也保證了開-閉原則,但是當類過多時會產(chǎn)生類"爆炸"的情況,所以具體選用什么模式需要根據(jù)實際需求進行取舍。

3. 抽象工廠模式

抽象工廠與工廠方法相比,抽象工廠允許生成不同的產(chǎn)品(即一個工廠存在多個產(chǎn)品)。代碼如下所示:

```cpp
#include <iostream>
using namespace std;
class Product
{
public:
	~Product() {}
	// 純虛函數(shù)
	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");
}

到此這篇關(guān)于C++設(shè)計模式中的工廠模式詳細介紹的文章就介紹到這了,更多相關(guān)C++工廠模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論