flyweight.cpp

来自「常见设计模式的解析和实现,c++实现,经典的设计模式解析源码」· C++ 代码 · 共 58 行

CPP
58
字号
/********************************************************************
	created:	2006/07/26
	filename: 	FlyWeight.cpp
	author:		李创
                http://www.cppblog.com/converse/

	purpose:	FlyWeight模式的演示代码
*********************************************************************/

#include "FlyWeight.h"
#include <iostream>

inline STATE Flyweight::GetIntrinsicState()
{
	return m_State;
}

FlyweightFactory::~FlyweightFactory()
{
	std::list<Flyweight*>::iterator iter1, iter2, temp;

	for (iter1 = m_listFlyweight.begin(), iter2 = m_listFlyweight.end();
		iter1 != iter2;
		)
	{
		temp = iter1;
		++iter1;
		delete (*temp);
	}

	m_listFlyweight.clear();
}

Flyweight* FlyweightFactory::GetFlyweight(const STATE& key)
{
	std::list<Flyweight*>::iterator iter1, iter2;

	for (iter1 = m_listFlyweight.begin(), iter2 = m_listFlyweight.end();
		 iter1 != iter2;
		 ++iter1)
	{
		if ((*iter1)->GetIntrinsicState() == key)
		{
			std::cout << "The Flyweight:" << key << " already exits"<< std::endl;
			return (*iter1);
		}
	}

	std::cout << "Creating a new Flyweight:" << key << std::endl;
	Flyweight* flyweight = new ConcreateFlyweight(key);
	m_listFlyweight.push_back(flyweight);
}

void ConcreateFlyweight::Operation(STATE& ExtrinsicState)
{

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?