C++設(shè)計(jì)模式中的工廠模式詳細(xì)介紹
1. 簡(jiǎn)單工廠模式
簡(jiǎn)單工廠模式(Simple Factory Pattern): 是指定義一個(gè)工廠類(lèi),工廠類(lèi)中實(shí)現(xiàn)一個(gè)方法,此方法根據(jù)不同的參數(shù)返回不同的類(lèi),UML類(lèi)圖如下所示:

代碼如下:
#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");
}簡(jiǎn)單工廠模式的問(wèn)題:
- 當(dāng)要?jiǎng)?chuàng)建的實(shí)例過(guò)多時(shí),會(huì)存在過(guò)多的if語(yǔ)句
- 當(dāng)要?jiǎng)?chuàng)建新的實(shí)例時(shí)要修改工廠方法,這樣做違背了開(kāi)-閉原則(即對(duì)擴(kuò)展開(kāi)放,對(duì)修改關(guān)閉的原則)
2. 工廠方法模式
工廠方法模式(Factory Method Pattern): 是在簡(jiǎn)單工廠模式的基礎(chǔ)上將工廠類(lèi)修改為抽象類(lèi),具體的類(lèi)實(shí)例創(chuàng)建交給抽象工廠的子類(lèi)。UML類(lèi)圖如所示:

代碼如下所示:
#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");
}工廠方法模式很好的避免了過(guò)多的if語(yǔ)句,同時(shí)也保證了開(kāi)-閉原則,但是當(dāng)類(lèi)過(guò)多時(shí)會(huì)產(chǎn)生類(lèi)"爆炸"的情況,所以具體選用什么模式需要根據(jù)實(shí)際需求進(jìn)行取舍。
3. 抽象工廠模式
抽象工廠與工廠方法相比,抽象工廠允許生成不同的產(chǎn)品(即一個(gè)工廠存在多個(gè)產(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è)計(jì)模式中的工廠模式詳細(xì)介紹的文章就介紹到這了,更多相關(guān)C++工廠模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++課程設(shè)計(jì)之學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++課程設(shè)計(jì)之學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
c++ Protobuf解決數(shù)據(jù)傳輸瓶頸面試精講
這篇文章主要介紹了c++ Protobuf解決數(shù)據(jù)傳輸瓶頸利器面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
QT連接Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟
本文主要介紹了QT連接Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
c++11 chrono全面解析(最高可達(dá)納秒級(jí)別的精度)
chrono是c++ 11中的時(shí)間庫(kù),本文就來(lái)詳細(xì)的介紹一下chrono庫(kù)的具體使用,關(guān)鍵是理解里面時(shí)間段(Durations)、時(shí)間點(diǎn)(Time points)的概念,感興趣的可以了解一下2021-11-11
C語(yǔ)言浮點(diǎn)型數(shù)據(jù)在內(nèi)存中的存儲(chǔ)方式詳解
任何數(shù)據(jù)在內(nèi)存中都是以二進(jìn)制的形式存儲(chǔ)的,例如一個(gè)short型數(shù)據(jù)1156,其二進(jìn)制表示形式為00000100 10000100,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言浮點(diǎn)型數(shù)據(jù)在內(nèi)存中的存儲(chǔ)方式,需要的朋友可以參考下2023-03-03
C++ 基礎(chǔ)編程之十進(jìn)制轉(zhuǎn)換為任意進(jìn)制及操作符重載
這篇文章主要介紹了C++ 基礎(chǔ)編程之十進(jìn)制轉(zhuǎn)換為任意進(jìn)制及操作符重載的相關(guān)資料,需要的朋友可以參考下2017-02-02
C語(yǔ)言中字符型數(shù)據(jù)和浮點(diǎn)型數(shù)據(jù)介紹
大家好,本篇文章主要講的是C語(yǔ)言中字符型數(shù)據(jù)和浮點(diǎn)型數(shù)據(jù)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽2022-01-01
解析C++中的虛擬函數(shù)及其靜態(tài)類(lèi)型和動(dòng)態(tài)類(lèi)型
虛擬函數(shù)(Visual Function)亦常被成為虛函數(shù),是C++中的一個(gè)重要特性,本文我們就來(lái)解析C++中的虛擬函數(shù)及其靜態(tài)類(lèi)型和動(dòng)態(tài)類(lèi)型2016-06-06

