1. UnsharedConcreteFlyweight(非共享具体享元类)
//UnsharedConcreteFlyweight(非共享具体享元类):坐标
class Coordinates
{
public:
Coordinates(int x,int y)
{
this->m_iX = x;
this->m_iY = y;
std::cout << "Coordinates 构造函数,X = " << x << " Y = " << y << std::endl;
}
~Coordinates()
{
std::cout << "~Coordinates 析构函数,X = " << this->m_iX << " Y = " << this->m_iY << std::endl;
}
void setX(int x)
{
this->m_iX = x;
}
int getX()
{
return m_iX;
}
void setY(int y)
{
this->m_iY = y;
}
int getY()
{
return m_iY;
}
private:
int m_iX;
int m_iY;
};
2. Flyweight(抽象享元类)
//Flyweight(抽象享元类):围棋棋子
class ChessPiece
{
public:
virtual ~ChessPiece() = default;
virtual std::string getColor() = 0;
void display(Coordinates *coord)
{
std::cout << "棋子颜色:" << getColor() << ",棋子位置;" << "X = " << coord->getX() << "Y = " << coord->getY() << std::endl;
}
protected:
ChessPiece() = default;
std::string m_sColor;
};
3. ConcreteFlyweight(具体享元类)
//ConcreteFlyweight(具体享元类)
class BlackChessPiece : public ChessPiece
{
public:
BlackChessPiece()
{
std::cout << "BlackChessPiece 构造函数" << std::endl;
m_sColor = "黑色";
}
~BlackChessPiece()
{
std::cout << "~BlackChessPiece 析构函数" << std::endl;
}
std::string getColor() override
{
return m_sColor;
}
private:
};
class WhiteChessPiece : public ChessPiece
{
public:
WhiteChessPiece()
{
std::cout << "WhiteChessPiece 构造函数" << std::endl;
m_sColor = "白色";
}
~WhiteChessPiece()
{
std::cout << "~WhiteChessPiece 析构函数" << std::endl;
}
std::string getColor() override
{
return m_sColor;
}
private:
};
4. FlyweightFactory(享元工厂类)
//FlyweightFactory(享元工厂类):ChessPieceFactory
class ChessPieceFactory
{
public:
static ChessPieceFactory *getInstance()
{
static ChessPieceFactory instance;
return &instance;
}
ChessPiece *getChessPiece(const std::string& color)
{
return m_mapChessPiece[color];
}
private:
ChessPieceFactory()
{
std::cout << "ChessPieceFactory 构造函数" << std::endl;
m_mapChessPiece.insert(std::pair("black", new BlackChessPiece));
m_mapChessPiece.insert(std::pair("white", new WhiteChessPiece));
}
~ChessPieceFactory()
{
std::cout << "~ChessPieceFactory 析构函数" << std::endl;
auto iter = m_mapChessPiece.begin();
while (iter != m_mapChessPiece.end())
{
ChessPiece *chessPiece = iter->second;
delete chessPiece;
iter++;
}
}
std::map m_mapChessPiece;
};
5. 主函数
/主函数///
void main()
{
ChessPiece *black1, *black2, *black3, *white1, *white2;
//获取享元工厂类
ChessPieceFactory *factory = ChessPieceFactory::getInstance();
//通过享元工厂获取三颗黑棋子
black1 = factory->getChessPiece("black");
black2 = factory->getChessPiece("black");
black3 = factory->getChessPiece("black");
std::cout << "两颗黑子是否相同:" << (black1 == black2) << std::endl;
//通过享元工厂获取两颗白棋子
white1 = factory->getChessPiece("white");
white2 = factory->getChessPiece("white");
std::cout << "两颗白子是否相同:" << (white1 == white2) << std::endl;
//函数模板类
std::vector coordinates;
auto func = [&coordinates](Coordinates *coord) {//std::function func = [&coordinates](Coordinates *coord ) {
coordinates.push_back(coord);
return coord;
};
//显示棋子坐标
black1->display(func(new Coordinates(1, 3)));
black2->display(func(new Coordinates(2, 6)));
black3->display(func(new Coordinates(4, 7)));
white1->display(func(new Coordinates(5, 8)));
white2->display(func(new Coordinates(4, 1)));
for (auto & coordinate : coordinates) {
delete coordinate;
}
system("pause");
}