C++设计模式实现--享元(Flyweight模式)

一、描述


      设计模式中的享元模式,避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类).


问题


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


其典型的结构图为: 



可以从图 2-1 中看出,Flyweight 模式中有一个类似 Factory 模式的对象构造工厂


      FlyweightFactory,当客户程序员(Client)需要一个对象时候就会向 FlyweightFactory 发出请求对象的消息 GetFlyweight()消息,FlyweightFactory 拥有一个管理、存储对象的“仓库”(或者叫对象池,vector 实现),GetFlyweight()消息会遍历对象池中的对象,如果已经存在则直接返回给 Client,否则创建一个新的对象返回给 Client。当然可能也有不想被共享的对象(例如结构图中的 UnshareConcreteFlyweight),但不在本模式的讲解范围,故在实现中不给出。 


二、实例


如上所描述的信息,创建类图:




本人的工程目录:


      


注释:

main:客户程序员(Client)

FlyweightFactory:“仓库”(对象池), 

Flyweight:对象池中的对象

ConcreteFlyweight:被共享的对象


代码:

仓库”(对象池):FlyweightFactory类

FlyweightFactory.h

[cpp]  view plain copy
  1. #ifndef __Flyweight__FlyweightFactory__  
  2. #define __Flyweight__FlyweightFactory__  
  3.   
  4. #include <iostream>  
  5. #include "Flyweight.h"  
  6. #include <string>  
  7. #include <vector>  
  8. #include "ConcreteFlyweight.h"  
  9. using std::cout;  
  10. using std::endl;  
  11. using std::string;  
  12. using std::vector;  
  13.   
  14. class FlyweightFactory  
  15. {  
  16. public:  
  17.     FlyweightFactory();  
  18.     ~FlyweightFactory();  
  19.     Flyweight* GetFlyweight(const string &key);  
  20. private:  
  21.     vector<Flyweight*> _fly;  
  22.       
  23. };  
  24.   
  25. #endif /* defined(__Flyweight__FlyweightFactory__) */  
FlyweightFactory.cpp

[cpp]  view plain copy
  1. #include "FlyweightFactory.h"  
  2.   
  3. FlyweightFactory::FlyweightFactory()  
  4. {  
  5.       
  6. }  
  7.   
  8. FlyweightFactory::~FlyweightFactory()  
  9. {  
  10.       
  11. }  
  12.   
  13. Flyweight* FlyweightFactory::GetFlyweight(const string &key)  
  14. {  
  15.     vector<Flyweight*>::iterator it = _fly.begin();  
  16.       
  17.     for (; it != _fly.end(); it++)  
  18.     {  
  19.         if ((*it)->GetIntrinsicState() == key)  
  20.         {  
  21.             cout<<"already created by users...."<<endl;  
  22.             return *it;  
  23.         }  
  24.   
  25.     }  
  26.     Flyweight *fn = new ConcreteFlyweight(key);  
  27.       
  28.     _fly.push_back(fn);  
  29.     return fn;  
  30.       
  31. }  

对象池中的对象:Flyweight类

Flyweight.h

[cpp]  view plain copy
  1. #ifndef __Flyweight__Flyweight__  
  2. #define __Flyweight__Flyweight__  
  3.   
  4. #include <iostream>  
  5. #include <string>  
  6. using std::string;  
  7.   
  8. class Flyweight  
  9. {  
  10. public:  
  11.     Flyweight(void);  
  12.     virtual ~Flyweight(void);  
  13.     virtual void Operation(const string& extrinsicState);  
  14.     string GetIntrinsicState();  
  15. protected:  
  16.     Flyweight(string intrinsicState);  
  17.       
  18. private:  
  19.     string _intrinsicState;  
  20.       
  21. };  
  22.   
  23.   
  24. #endif /* defined(__Flyweight__Flyweight__) */  
Flyweight.cpp

[cpp]  view plain copy
  1. #include "Flyweight.h"  
  2.   
  3. Flyweight::Flyweight(void)  
  4. {  
  5.       
  6. }  
  7.   
  8. Flyweight::Flyweight(string intrinsicState)  
  9. {  
  10.     this->_intrinsicState = intrinsicState;  
  11. }  
  12.   
  13. Flyweight::~Flyweight()  
  14. {  
  15.       
  16. }  
  17.   
  18. void Flyweight::Operation(const string &extrinsicState)  
  19. {  
  20.       
  21. }  
  22.   
  23. string Flyweight::GetIntrinsicState()  
  24. {  
  25.     return this->_intrinsicState;  
  26. }  


被共享的对象:ConcreteFlyweight

ConcreteFlyweight.h

[cpp]  view plain copy
  1. #ifndef __Flyweight__ConcreteFlyweight__  
  2. #define __Flyweight__ConcreteFlyweight__  
  3.   
  4. #include <iostream>  
  5. #include "Flyweight.h"  
  6. class ConcreteFlyweight:public Flyweight  
  7. {  
  8. public:  
  9.     ConcreteFlyweight(void);  
  10.     ConcreteFlyweight(string intrinsicState);  
  11.     ~ConcreteFlyweight();  
  12.     void Operation(const string& extrinsicState);  
  13.       
  14. };  
  15.   
  16. #endif /* defined(__Flyweight__ConcreteFlyweight__) */  
ConcreteFlyweight.cpp

[cpp]  view plain copy
  1. #include "ConcreteFlyweight.h"  
  2. using std::cout;  
  3. using std::endl;  
  4.   
  5. ConcreteFlyweight::ConcreteFlyweight(void)  
  6. {  
  7.       
  8. }  
  9.   
  10. ConcreteFlyweight::ConcreteFlyweight(string intrinsicState)  
  11. {  
  12.     cout<<"ConcreteFlyweight Build....."<<intrinsicState<<endl;  
  13. }  
  14.   
  15. ConcreteFlyweight::~ConcreteFlyweight()  
  16. {  
  17.       
  18. }  
  19.   
  20. void ConcreteFlyweight::Operation(const string &extrinsicState)  
  21. {  
  22.     cout<<"ConcreteFlyweight:内蕴["<<this->GetIntrinsicState()<<"] 外 蕴["<<extrinsicState<<"]"<<endl;  
  23. }  

客户程序员(Client)

main.cpp

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include "Flyweight.h"  
  3. #include "ConcreteFlyweight.h"  
  4. #include "FlyweightFactory.h"  
  5. using namespace std;  
  6.   
  7.   
  8. int main(int argc, const char * argv[])  
  9. {  
  10.   
  11.     FlyweightFactory *fc = new FlyweightFactory();  
  12.     Flyweight* fw1 = fc->GetFlyweight("hello");  
  13.     Flyweight* fw2 = fc->GetFlyweight("world!");  
  14.     Flyweight* fw3 = fc->GetFlyweight("hello2");  
  15.     // insert code here...  
  16.     std::cout << "Hello, World!\n";  
  17.     return 0;  
  18. }  

结果如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值