Flyweight
来源:互联网 发布:cocos2d js引擎 编辑:程序博客网 时间:2024/06/05 23:59
#ifndef FLYWEIGHT_H
#define FLYWEIGHT_H
#include <string>
#include <list>
typedef std::string STATE;
class Flyweight
{
public:
virtual ~Flyweight(){}
STATE GetIntrinsicState();
virtual void Operation(STATE& ExtrinsicState) = 0;
protected:
Flyweight(const STATE& state)
:m_State(state)
{
}
private:
STATE m_State;
};
class FlyweightFactory
{
public:
FlyweightFactory(){}
~FlyweightFactory();
Flyweight* GetFlyweight(const STATE& key);
private:
std::list<Flyweight*>m_listFlyweight;
};
class ConcreateFlyweight
: public Flyweight
{
public:
ConcreateFlyweight(const STATE& state)
: Flyweight(state)
{
}
virtual ~ConcreateFlyweight(){}
virtual void Operation(STATE& ExtrinsicState);
};
#endif
实现文件
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);
return flyweight;
}
void ConcreateFlyweight::Operation(STATE& ExtrinsicState)
{
}
main函数的调用:
int main()
{
FlyweightFactory flyweightfactory;
flyweightfactory.GetFlyweight("hello");
flyweightfactory.GetFlyweight("world");
flyweightfactory.GetFlyweight("hello");
system("pause");
return 0;
}
输出结果:
个人的建议:采用智能指针改进,即链表list存放智能指针:
注:以下粉红色部分未更改部分,其余不变。
class FlyweightFactory
{
...
private:
std::list<shared_ptr<Flyweight>>m_listFlyweight;
};
FlyweightFactory::~FlyweightFactory()
{
m_listFlyweight.clear();
}
Flyweight* FlyweightFactory::GetFlyweight(const STATE& key)
{
std::list<shared_ptr<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;
shared_ptr<Flyweight>flyweight= new ConcreateFlyweight(key);
m_listFlyweight.push_back(flyweight);
return *flyweight;
}
- Flyweight
- Flyweight
- Flyweight
- flyweight
- flyweight
- flyweight
- Flyweight
- Flyweight
- Flyweight Pattern
- flyweight模式
- FlyWeight Pattern
- Flyweight模式
- FlyWeight模式
- Flyweight模式
- Flyweight模式
- Flyweight Design
- Flyweight 模式
- Flyweight模式
- Android中的长度单位详解(dp、sp、px、in、pt、mm)
- oracle tablespace&datafile -- oracle表空间
- jQuery Ajax例子
- 电话号码分割js(修改)
- usaco 月赛 2008 January Best Cow Line 贝茜的晨练计划 题解
- Flyweight
- CToolTipCtrl 背景颜色字体颜色设置无效解决方案
- Exadata混合列压缩如何处理INSERT和UPDATE
- js/jquery 跨站提交
- java中List.subList方法使用注意
- Codeforces_389B_Fox and Cross(贪心)
- NEsper事件处理Demo
- 程序员的奋斗史(二十)——沉思录(一)——关于博客和思想
- structs2 中链接引用的绝对路径