设计模式C++描述----12.享元(Flyweight)模式
一. 概述
在面向对象系统的设计何实现中,创建对象是最为常见的操作。
这里面就有一个问题:如果一个应用程序使用了太多的对象,就会造成很大的存储开销。特别是对于大量轻量级(细粒度)的对象,比如在文档编辑器的设计过程中,我们如果没有为字母创建一个对象的话,系统可能会因为大量的对象而造成存储开销的浪费。
例如一个字母“a”在文档中出现了100000次,而实际上我们可以让这一万个字母“a”共享一个对象,当然因为在不同的位置可能字母“a”有不同的显示效果(例如字体和大小等设置不同),在这种情况我们可以为将对象的状态分为“外部状态”和“内部状态”,将可以被共享(不会变化)的状态作为内部状态存储在对象中,而外部对象(例如上面提到的字体、大小等)我们可以在适当的时候将外部对象最为参数传递给对象(例如在显示的时候,将字体、大小等信息传递给对象)。
二. 享元模式
定义:运用共享技术有效地支持大量细粒度的对象。
结构图如下:
Flyweight:所有具体享元类的父类,或接口
ConcreteFlyweight:具体享元类,实现具体的操作
UnshareConcreteFlyweight:不需要共享的子类
FlyweightFactory:合理的创建并管理享元类
代码如下:
[cpp] view plaincopyprint?//享元类
class Flyweight
{
public:
virtual ~Flyweight() {}
virtual void Operation(const string& extrinsicState) {}
string GetIntrinsicState()
{
return this->_intrinsicState;
}
protected:
Flyweight(string intrinsicState)
{
this->_intrinsicState = intrinsicState;
}
private:
string _intrinsicState;
};
//具体享元类
class ConcreteFlyweight:public Flyweight
{
public:
ConcreteFlyweight(string intrinsicState):Flyweight(intrinsicState)
{
cout<<"ConcreteFlyweight Build....."<<intrinsicState<<endl;
}
~ConcreteFlyweight() {}
//实现接口
void Operation(const string& extrinsicState)
{
cout<<"内部["<<this->GetIntrinsicState()<<"] 外部["<<extrinsicState<<"]"<<endl;
}
};
//享元工厂
class FlyweightFactory
{
public:
FlyweightFactory() {}
~FlyweightFactory() {}
//确保合理的共享 Flyweight
Flyweight* GetFlyweight(const string& key)
{
vector<Flyweight*>::iterator it = _fly.begin();
for (; it != _fly.end();it++)
{
if ((*it)->GetIntrinsicState() == key)
{
cout<<"already created by users...."<<endl;
return *it;
}
}
Flyweight* fn = new ConcreteFlyweight(key);
_fly.push_back(fn);
return fn;
}
private:
vector<Flyweight*> _fly;
};
//测试
int main(int argc,char* argv[])
{
FlyweightFactory* fc = new FlyweightFactory();
//不同的对象,享元工厂将会创建新的享元类
Flyweight* fw1 = fc->GetFlyweight("Object A");
Flyweight* fw2 = fc->GetFlyweight("Object B");
//相同的对象,享元工厂将会使用一个已创建的享元类
Flyweight* fw3 = fc->GetFlyweight("Object A");
return 0;
}
//享元类
class Flyweight
{
public:
virtual ~Flyweight() {}
virtual void Operation(const string& extrinsicState) {}
string GetIntrinsicState()
{
return this->_intrinsicState;
}
protected:
Flyweight(string intrinsicState)
{
this->_intrinsicState = intrinsicState;
}
private:
string _intrinsicState;
};
//具体享元类
class ConcreteFlyweight:public Flyweight
{
public:
ConcreteFlyweight(string intrinsicState):Flyweight(intrinsicState)
{
cout<<"ConcreteFlyweight Build....."<<intrinsicState<<endl;
}
~ConcreteFlyweight() {}
//实现接口
void Operation(const string& extrinsicState)
{
cout<<"内部["<<this->GetIntrinsicState()<<"] 外部["<<extrinsicState<<"]"<<endl;
}
};
//享元工厂
class FlyweightFactory
{
public:
FlyweightFactory() {}
~FlyweightFactory() {}
//确保合理的共享 Flyweight
Flyweight* GetFlyweight(const string& key)
{
vector<Flyweight*>::iterator it = _fly.begin();
for (; it != _fly.end();it++)
{
if ((*it)->GetIntrinsicState() == key)
{
cout<<"already created by users...."<<endl;
&nbs
补充:软件开发 , C++ ,