为什么需要享元模式
在软件系统采用纯粹对象方案的问题在于大量细粒度的对象会很快充斥在系统中,从而带来很高的运行时代价---主要指内存需要方面的代价。
什么是享元模式
运用共享技术有效地支持大量细粒度的对象。
类图
示例
class Font {//将字体实现为对象
private:
//unique object key
string key;
//object state
//....
public:
Font(const string& key){
//...
}
};
class FontFactory{
private:
map<string,Font* > fontPool;//字体库
public:
Font* GetFont(const string& key){//对象池
map<string,Font*>::iterator item=fontPool.find(key);