Flyweight 模式

时间:2022-11-26 21:25:14

如果一个应用程序使用了太多的对象, 就会造成很大的存储开销。 特别是对于大量轻量级 (细粒度)的对象,比如在文档编辑器的设计过程中,我们如果为每个字母创建一个对象的话,系统可能会因为大量的对象而造成存储开销的浪费。例如一个字母“a”在文档中出现了100000 次,而实际上我们可以让这一万个字母“a”共享一个对象,当然因为在不同的位置可能字母“a”有不同的显示效果(例如字体和大小等设置不同) ,在这种情况我们可以为将对象的状态分为“外部状态”和“内部状态” ,将可以被共享(不会变化)的状态作为内部状态存储在对象中,而外部对象(例如上面提到的字体、大小等)我们可以在适当的时候将外部对象最为参数传递给对象(例如在显示的时候,将字体、大小等信息传递给对象) 。

Flyweight 模式可以解决上面的问题,其典型的结构图为:

Flyweight 模式

 ///////////////Flyweight.h////////////////////////
#pragma once
#include <string>
using namespace std;
class Flyweight
{
public:
virtual ~Flyweight();
virtual void Operation(const string& extrinsicState);
string GetIntrinsicState();
protected:
Flyweight(string intrinsicState);
private:
string _intrinsicState ;
}; class ConcreteFlyweight : public Flyweight
{
public:
~ConcreteFlyweight();
ConcreteFlyweight(string intrinsicState);
void Operation(const string& extrinsicState);
protected:
private:
};
 ///////////////Flyweight.cpp//////////////////////////
#include "Flyweight.h"
#include <iostream>
using namespace std; Flyweight::Flyweight(string intrinsicState)
{
_intrinsicState = intrinsicState ;
}
Flyweight::~Flyweight()
{ } void Flyweight::Operation(const string& extrinsicState)
{ }
string Flyweight::GetIntrinsicState()
{
return this->_intrinsicState ;
} ConcreteFlyweight::ConcreteFlyweight(string intrinsicState):Flyweight(intrinsicState)
{
cout<<"ConcreteFlyweight Build....."<<intrinsicState<<endl;
}
ConcreteFlyweight::~ConcreteFlyweight()
{ }
void ConcreteFlyweight::Operation(const string& extrinsicState)
{
cout<<"ConcreteFlyweight: 内部["<<this->GetIntrinsicState()<<"] 外部["<<extrinsicState<<"]"<<endl;
}
 /////////////////FlyweightFactory.h////////////////////////////
#pragma once
#include "Flyweight.h"
#include <vector>
class FlyweightFactory
{
public:
FlyweightFactory();
~FlyweightFactory();
Flyweight* GetFlyweight(const string& key); protected:
private:
vector<Flyweight*> _fly ;
};
 ///////////////////FlyweightFactory.cpp//////////////////////////////
#include "FlyweightFactory.h"
#include <iostream>
using namespace std;
FlyweightFactory::FlyweightFactory()
{ }
FlyweightFactory::~FlyweightFactory()
{ } Flyweight* FlyweightFactory::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 ; }
 /////////////////////////main.cpp/////////////////////////////////////////////////
#include "FlyweightFactory.h"
#include "Flyweight.h"
#include <iostream>
using namespace std;
int main()
{
FlyweightFactory* fc = new FlyweightFactory();
Flyweight* fw1 = fc->GetFlyweight("hello");
Flyweight* fw2 = fc->GetFlyweight("world"); cout<<fw1->GetIntrinsicState()<<endl;
fw1->Operation("red"); cout<<fw2->GetIntrinsicState()<<endl;
fw2->Operation("green"); fc->GetFlyweight("hello")->Operation("black");
getchar();
return ;
}