網(wǎng)站首頁 編程語言 正文
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)建新的實例時要修改工廠方法,這樣做違背了開-閉原則(即對擴(kuò)展開放,對修改關(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ù)實際需求進(jìn)行取舍。
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");
}
原文鏈接:https://blog.csdn.net/weixin_41111116/article/details/120399588
相關(guān)推薦
- 2022-06-13 C++IO流之fstream,?stringstream使用小結(jié)_C 語言
- 2023-04-19 error:13: Permission denied (nginx 查看日志 failed (13
- 2022-05-10 thymeleaf給響應(yīng)頁面?zhèn)鬟f參數(shù)(modelandview 中的model)
- 2022-12-13 Python按天實現(xiàn)生成時間范圍序列的方法詳解_python
- 2022-03-27 C++命名空間和缺省參數(shù)介紹_C 語言
- 2022-11-18 Go與Redis實現(xiàn)分布式互斥鎖和紅鎖_Golang
- 2022-06-24 淺談Golang的new與make區(qū)別是什么_Golang
- 2022-08-23 一文教會你調(diào)整Matplotlib子圖的大小_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支