设计模式的理会和实现(C++)之十三-FlyWeight模式
副标题#e#
浸染:
运用共享技能有效地支持大量细粒度的工具。
UML布局图:
理会:
Flyweight模式在大量利用一些可以被共享的工具的时候常常利用.好比,在QQ谈天的时候许多时候你懒得回覆又不得不回覆的时候,一般会用一些客气的话语对于别人,如"呵呵","好的"等等之类的,这些简朴的复原其实每小我私家都是提前界说好的,在利用的时候才挪用出来.Flyweight就是基于办理这种问题的思路而发生的,当需要一个可以在其它处所共享利用的工具的时候,先去查询是否已经存在了同样的工具,假如没有就生成之有的话就直接利用.因此,Flyweight模式和Factory模式也常常混用.
#p#副标题#e#
实现:
需要说明的是下面的实现仅仅实现了对可共享工具的利用,非可共享工具的利用没有列出,因为这个不是Flyweight模式的重点.这里的实现要点是回收一个list链表来生存这些可以被共享的工具,需要利用的时候就到链表中查询是不是已经存在了,假如不存在就初始化一个,然后返回这个工具的指针.
1)Flyweight.h
/**//********************************************************************
created: 2006/07/26
filename: FlyWeight.h
author: 李创
http://www.cppblog.com/converse/
purpose: FlyWeight模式的演示代码
*********************************************************************/
#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
2)Flyweight.cpp
/**//********************************************************************
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)
{
}
3)Main.cpp
#p#分页标题#e#
/**//********************************************************************
created: 2006/07/26
filename: Main.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: FlyWeight模式的测试代码
*********************************************************************/
#include "FlyWeight.h"
int main()
{
FlyweightFactory flyweightfactory;
flyweightfactory.GetFlyweight("hello");
flyweightfactory.GetFlyweight("world");
flyweightfactory.GetFlyweight("hello");
system("pause");
return 0;
}