最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++設計模式中的工廠模式詳細介紹

 更新時間:2022年09月01日 09:51:25   作者:憤怒的小黃鴨  
工廠模式,是一種實例化對象的方式,只要輸入需要實例化對象的名字,就可以通過工廠對象的相應工廠函數(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)建新的實例時要修改工廠方法,這樣做違背了開-閉原則(即對擴展開放,對修改關閉的原則)

2. 工廠方法模式

工廠方法模式(Factory Method Pattern): 是在簡單工廠模式的基礎上將工廠類修改為抽象類,具體的類實例創(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");
}

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

相關文章

  • C語言順序表的基本結(jié)構與實現(xiàn)思路詳解

    C語言順序表的基本結(jié)構與實現(xiàn)思路詳解

    順序表是用一段物理地址連續(xù)的存儲單元依次存儲數(shù)據(jù)元素的線性結(jié)構,一般情況下采用數(shù)組存儲。本文將通過示例為大家講解一下順序表的基本操作,需要的可以參考一下
    2023-02-02
  • 詳解C語言初階基礎

    詳解C語言初階基礎

    這篇文章主要介紹了C語言中的初階基礎,介紹了其相關概念,具有一定參考價值。需要的朋友可以了解下,希望能夠給你帶來幫助
    2021-11-11
  • c語言中實現(xiàn)數(shù)組幾個數(shù)求次大值

    c語言中實現(xiàn)數(shù)組幾個數(shù)求次大值

    這篇文章主要介紹了c語言中實現(xiàn)數(shù)組幾個數(shù)求次大值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • C++實現(xiàn)點云添加高斯噪聲功能

    C++實現(xiàn)點云添加高斯噪聲功能

    所謂高斯噪聲是指它的概率密度函數(shù)服從高斯分布(即正態(tài)分布)的一類噪聲,這篇文章主要給大家介紹了關于C++實現(xiàn)點云添加高斯噪聲功能的相關資料,需要的朋友可以參考下
    2021-07-07
  • 詳解C++ 中的臨時對象

    詳解C++ 中的臨時對象

    這篇文章主要介紹了C++ 中的臨時對象的相關資料,幫助大家更好的理解和學習c++,感興趣的朋友可以了解下
    2020-08-08
  • C語言編程實例之輸出指定圖形問題

    C語言編程實例之輸出指定圖形問題

    這篇文章主要介紹了C語言編程實例之輸出指定圖形問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 利用C語言實現(xiàn)“百馬百擔”問題方法示例

    利用C語言實現(xiàn)“百馬百擔”問題方法示例

    百馬百擔是道經(jīng)典的算法題,下面這篇文章主要給大家介紹了利用C語言實現(xiàn)“百馬百擔”問題的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-12-12
  • c++實現(xiàn)獲取當前時間(精確至秒,毫秒和微妙)

    c++實現(xiàn)獲取當前時間(精確至秒,毫秒和微妙)

    這篇文章主要為大家詳細介紹了c++實現(xiàn)獲取當前時間(可以精確至秒,毫秒和微妙)的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
    2023-11-11
  • C語言模擬實現(xiàn)掃雷游戲

    C語言模擬實現(xiàn)掃雷游戲

    這篇文章主要為大家詳細介紹了C語言模擬實現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • C語言二分查找算法及實現(xiàn)代碼

    C語言二分查找算法及實現(xiàn)代碼

    本文主要介紹C語言的二分查找算法,這里給大家詳細介紹了什么是二分查找,并提供代碼實例,需要的小伙伴可以參考下
    2016-07-07

最新評論

兖州市| 沅江市| 德阳市| 武穴市| 永川市| 上饶县| 班戈县| 孝感市| 陇川县| 永嘉县| 湖南省| 大名县| 星子县| 凤凰县| 额济纳旗| 太湖县| 万源市| 清流县| 温泉县| 政和县| 阜宁县| 遂平县| 华蓥市| 上饶市| 毕节市| 莱阳市| 宝兴县| 万宁市| 大连市| 若尔盖县| 蓬安县| 郎溪县| 神农架林区| 呼伦贝尔市| 新宾| 凯里市| 阿拉善左旗| 邯郸市| 北流市| 丰城市| 台南市|