Flyweight 模式
2014-05-11 22:25
204 查看
如果一个应用程序使用了太多的对象, 就会造成很大的存储开销。 特别是对于大量轻量级 (细粒度)的对象,比如在文档编辑器的设计过程中,我们如果为每个字母创建一个对象的话,系统可能会因为大量的对象而造成存储开销的浪费。例如一个字母“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 0; }
相关文章推荐
- Flyweight模式
- 设计模式:享元模式-flyweight
- 设计模式之享元模式(Flyweight)
- 享元模式Flyweight——结构型模式
- 设计模式(十)享元模式Flyweight(结构型)
- JAVA设计模式(23) —享元(FlyWeight)模式
- 设计模式-享元模式-flyweight-python??
- 黑马程序员- 高新技术-1.5新特性之基本数据类型自动装箱、享源设计模式 flyweight
- Java中的享元(flyweight)设计模式
- 设计模式(十一)之 Flyweight(享元)
- 享元(Flyweight)模式
- 设计模式学习笔记(十二)——Flyweight享元模式
- 黑马程序员_03_享元设计模式Flyweight
- FlyWeight模式
- 设计模式理解(十)结构型——享元(Flyweight)
- Java共享模式/享元模式(Flyweight模式)
- Java设计模式-享元模式(Flyweight)
- 23种设计模式之python实现--Flyweight模式
- 设计模式-FlyWeight
- 设计模式学习笔记--享元(Flyweight)模式