在面向对象系统的设计何实现中,创建对象是最为常见的操作。这里面就有一个问题:如果一个应用程序使用了太多的对象,就会造成很大的存储开销。特别是对于大量轻量级(细粒度)的对象,比如在文档编辑器的设计过程中,我们如果为没有字母创建一个对象的话,系统可能会因为大量的对象而造成存储开销的浪费。例如一个字母“a”在文档中出现了100000 次,而实际上我们可以让这一万个字母“a”共享一个对象,当然因为在不同的位置可能字母“a”有不同的显示效果(例如字体和大小等设置不同),在这种情况我们可以为将对象的状态分为“外部状态”和“内部状态”,将可以被共享(不会变化)的状态作为内部状态存储在对象中,而外部对象(例如上面提到的字体、大小等)我们可以在适当的时候将外部对象最为参数传递给对象(例如在显示的时候,将字体、大小等信息传递给对象)。
从图 2-1 中看出,Flyweight 模式中有一个类似 Factory 模式的对象构造工厂FlyweightFactory,当客户程序员(Client)需要一个对象时候就会向 FlyweightFactory 发出请求对象的消息 GetFlyweight()消息,FlyweightFactory 拥有一个管理、存储对象的“仓库”(或者叫对象池,vector 实现),GetFlyweight()消息会遍历对象池中的对象,如果已经存在则直接返回给 Client,否则创建一个新的对象返回给 Client。当然可能也有不想被共享的对象(例如结构图中的 UnshareConcreteFlyweight),但不在本模式的讲解范围,故在实现中不给出
//Flyweight.h
#pragma once
#include <string>
#include <memory>
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(string intrinsicState);
~ConcreteFlyweight();
void Operation(const string& extrinsicState);
protected:
private:
};
//Flyweight.cpp
#include "stdafx.h"
#include "Flyweight.h"
#include <iostream>
using namespace std;
Flyweight::Flyweight(string intrinsicState)
{
this->_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 <string>
#include <vector>
#include <memory>
using namespace std;
class FlyweightFactory
{
public:
FlyweightFactory();
~FlyweightFactory();
shared_ptr<Flyweight> GetFlyweight(const string& key);
//Flyweight* GetFlyweight(const string& key);
protected:
private:
//vector<Flyweight*> _fly;
vector<shared_ptr<Flyweight>> _fly;
};
//FlyweightFactory.cpp
#include "stdafx.h"
#include "FlyweightFactory.h"
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
using namespace std;
FlyweightFactory::FlyweightFactory()
{
}
FlyweightFactory::~FlyweightFactory()
{
}
shared_ptr<Flyweight> FlyweightFactory::GetFlyweight(const string& key)
{
vector<shared_ptr<Flyweight>>::iterator it = _fly.begin();
for (; it != _fly.end();it++)
{
//找到了,就一起用,^_^
if ((*it)->GetIntrinsicState() == key)
{
cout<<"already created by users...."<<endl;
return *it;
}
}
//shared_ptr<Flyweight> fn(new ConcreteFlyweight(key));
shared_ptr<Flyweight> fn = make_shared<ConcreteFlyweight>(key);
_fly.push_back(fn);
return fn;
}
int main(int argc, _TCHAR* argv[])
{
shared_ptr<FlyweightFactory> fc = make_shared<FlyweightFactory>();
shared_ptr<Flyweight> fw1 = fc->GetFlyweight("hello");
shared_ptr<Flyweight> fw2 = fc->GetFlyweight("world!");
shared_ptr<Flyweight> fw3 = fc->GetFlyweight("hello");
return 0;
}
Flyweight 模式在实现过程中主要是要为共享对象提供一个存放的“仓库”(对象池),这里是通过 C++ STL 中 Vector 容器,当然就牵涉到 STL 编程的一些问题(Iterator 使用等)。另外应该注意的就是对对象“仓库”(对象池)的管理策略(查找、插入等),这里是通过直接的顺序遍历实现的,当然我们可以使用其他更加有效的索引策略,例如 Hash 表的管理策略,当时这些细节已经不是 Flyweight 模式本身要处理的了。
我们在 State 模式和 Strategy 模式中会产生很多的对象,因此我们可以通过 Flyweight模式来解决这个问题。