C++設(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");
}簡單工廠模式的問題:
- 當(dāng)要創(chuàng)建的實例過多時,會存在過多的if語句
- 當(dāng)要創(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語句,同時也保證了開-閉原則,但是當(dāng)類過多時會產(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)文章
C語言順序表的基本結(jié)構(gòu)與實現(xiàn)思路詳解
順序表是用一段物理地址連續(xù)的存儲單元依次存儲數(shù)據(jù)元素的線性結(jié)構(gòu),一般情況下采用數(shù)組存儲。本文將通過示例為大家講解一下順序表的基本操作,需要的可以參考一下2023-02-02
c語言中實現(xiàn)數(shù)組幾個數(shù)求次大值
這篇文章主要介紹了c語言中實現(xiàn)數(shù)組幾個數(shù)求次大值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
利用C語言實現(xiàn)“百馬百擔(dān)”問題方法示例
百馬百擔(dān)是道經(jīng)典的算法題,下面這篇文章主要給大家介紹了利用C語言實現(xiàn)“百馬百擔(dān)”問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
c++實現(xiàn)獲取當(dāng)前時間(精確至秒,毫秒和微妙)
這篇文章主要為大家詳細介紹了c++實現(xiàn)獲取當(dāng)前時間(可以精確至秒,毫秒和微妙)的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2023-11-11

