栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

设计模式之Flyweight享元模式

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设计模式之Flyweight享元模式

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");
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316605.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号