您的位置:首页 > 其它

设计模式 - Flyweight

2013-05-05 16:56 176 查看
意图:运用共享技术有效地支持大量细粒度的对象。

class Flyweight
{
public:
Flyweight()
{}
virtual ~Flyweight()
{}
virtual void Operation(int extrinsic_state) = 0;
};

class ConcreteFlyweight: public Flyweight
{
public:
ConcreteFlyweight(const string& key)
{
this->key = key;
}
~ConcreteFlyweight()
{}
void Operation(int extrinsic_state)
{
cout<<"ConcreteFlyweight::Operation()"<<endl;
}

private:
string key;
};

class FlyweightFactory
{
public:
FlyweightFactory()
{}
~FlyweightFactory()
{}
Flyweight* GetFlyweight(const string& key)
{
if(flyweightMap.find(key) == flyweightMap.end())
{
flyweightMap[key] = new ConcreteFlyweight(key);
}
return flyweightMap[key];
}

private:
map<string, Flyweight*> flyweightMap;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: