C++享元模式詳解

#include <iostream>
#include <list>
#include <map>
using namespace std;

enum class  EnumColor //棋子類型
{
	Black, //黑
	White  //白
};
struct Position //棋子位置
{
	int m_x;
	int m_y;
	Position(int tmpx, int tmpy) :m_x(tmpx), m_y(tmpy) {} //構造函數			
};
/*棋子類*/
class Piece 
{
public:
	//構造函數
	Piece(EnumColor tmpcolor, Position tmppos) :m_color(tmpcolor), m_pos(tmppos){}
	//棋子的繪制
	void draw()
	{
		if(m_color == EnumColor::Black)
		{
			cout << "在位置:(" << m_pos.m_x << "," << m_pos.m_y << ")處繪制瞭一個黑色棋子!" << endl;
		}
		else
		{
			cout << "在位置:(" << m_pos.m_x << "," << m_pos.m_y << ")處繪制瞭一個白色棋子!" << endl;
		}
	}
private:
	EnumColor m_color; //棋子顏色
	Position m_pos; //棋子位置
};
int main()
{
	//檢測內存泄漏
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	Piece* p_piece1 = new Piece(EnumColor::Black, Position(3, 3)); //黑色落子到3,3位置
	p_piece1->draw();
	Piece* p_piece2 = new Piece(EnumColor::White, Position(5, 5)); //白色落子到5,5位置
	p_piece2->draw();
	Piece* p_piece3 = new Piece(EnumColor::Black, Position(4, 6)); //黑色落子到4,6位置
	p_piece3->draw();
	Piece* p_piece4 = new Piece(EnumColor::White, Position(5, 7)); //白色落子到5,7位置
	p_piece4->draw();
	//釋放資源
	delete p_piece1;
	delete p_piece2;
	delete p_piece3;
	delete p_piece4;
	return 0;
}

在這裡插入圖片描述

對於上面的例程來說,棋子的類型無非就是兩種,黑和白,在實際的下棋過程當中,隻有位置是不一樣的,但是卻衍生出瞭多個具有相似度的對象,我們能否進行改造一下,比如,對象隻有兩個,黑和白,在實際的運轉的時候,改變他們的位置就可以瞭,這樣就不用new很多給相似的對象出來瞭。下面我們進行改造一下。引入享元模式。

#include <iostream>
#include <list>
#include <map>
using namespace std;

enum EnumColor //棋子類型
{
	Black, //黑
	White  //白
};
struct Position //棋子位置
{
	int m_x;
	int m_y;
	Position(int tmpx, int tmpy) :m_x(tmpx), m_y(tmpy) {} //構造函數			
};
class Piece //棋子抽象類
{
public:
	virtual ~Piece() {} 
public:
	virtual void draw(Position tmppos) = 0;
};

class BlackPiece :public Piece //黑色棋子
{
public:
	virtual void draw(Position tmppos)
	{
		cout << "在位置:(" << tmppos.m_x << "," << tmppos.m_y << ")處繪制瞭一個黑色棋子!" << endl;
	}
};

class WhitePiece :public Piece //白色棋子
{
public:
	virtual void draw(Position tmppos)
	{
		cout << "在位置:(" << tmppos.m_x << "," << tmppos.m_y << ")處繪制瞭一個白色棋子!" << endl;
	}
};

class pieceFactory //創建棋子的工廠
{
public:
	virtual ~pieceFactory()
	{
		//釋放內存
		for(auto iter = m_FlyWeightMap.begin(); iter != m_FlyWeightMap.end(); ++iter)
		{
			Piece* tmpfw = iter->second;
			delete tmpfw;
		}
		m_FlyWeightMap.clear(); 
	}
	//獲取享元對象,也就是獲取被共享的棋子對象
	Piece* getFlyWeight(EnumColor tmpcolor)
	{
		auto iter = m_FlyWeightMap.find(tmpcolor);
		if(iter == m_FlyWeightMap.end())
		{
			//沒有該享元對象,那麼就創建出來
			Piece* tmpfw = nullptr;
			if(tmpcolor == Black) //黑子
			{
				tmpfw = new BlackPiece();
			}
			else //白子
			{
				tmpfw = new WhitePiece();
			}
			//以棋子顏色枚舉值作為key,增加條目到map中
			m_FlyWeightMap.insert(make_pair(tmpcolor, tmpfw)); 
			return tmpfw;
		}
		else
		{
			return iter->second;
		}
	}
private:
	std::map<EnumColor, Piece*> m_FlyWeightMap; //享元池,用map容器來保存所有的享元對象,一共就兩個享元對象(黑色棋子一個,白色棋子一個)
};
int main()
{
	//檢測內存泄漏
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	pieceFactory* pfactory = new pieceFactory();
	Piece* p_piece1 = pfactory->getFlyWeight(Black);
	p_piece1->draw(Position(3, 3));  //黑子落子到3,3位置
	Piece* p_piece2 = pfactory->getFlyWeight(White);
	p_piece2->draw(Position(5, 5));  //白子落子到5,5位置
	Piece* p_piece3 = pfactory->getFlyWeight(Black);
	p_piece3->draw(Position(4, 6));  //黑子落子到4,6位置
	Piece* p_piece4 = pfactory->getFlyWeight(White);
	p_piece4->draw(Position(5, 7));  //白子落子到5,7位置
	//釋放資源
	delete  pfactory;
	return 0;
}

在這裡插入圖片描述

在這裡插入圖片描述

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: