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;
}
标签:享元,draw,int,EnumColor,模式,棋子,c++,Position,Piece 来源: https://blog.csdn.net/qq_38158479/article/details/120257514