现在的位置: 首页 > 综合 > 正文

常见设计模式解析和实现(C++)FlyWeight模式

2016年06月22日 ⁄ 综合 ⁄ 共 2651字 ⁄ 字号 评论关闭

作用:运用共享技术有效地支持大量细粒度的对象

 

UML结构图:

 

解析:

Flyweight模式在大量使用一些可以被共享的对象的时候使用。比如,在QQ聊天时很多时候你懒得回复又不得不回复,一般会用一些客套的话语敷衍别人,如“呵呵”,“好的”等待之类的,这些简单的答复其实每个人都是提前定义好的,在使用的时候才调用起来。

Flyweight就是基于解决这种问题的思路而产生的,当需要一个可以在其他地方共享使用的对象的时候,先去查询是否已经存在了同样的对象,如果没有就生成之;有的话就直接使用

因此,Flyweight模式和Factory模式也经常混用。

 

实现:

需要说明的是下面的实现仅仅实现了对可共享对象的使用,非可共享对象的使用没有列出,因为这个不是Flyweight模式的重点。

这里的实现要点就是采用一个list链表来保存这些可以被共享的对象,需要使用的时候就到链表中查询是不是已经存在了,如果不存在就初始化一个,然后返回这个对象的指针。

 (1)Flywight.h

  1. #include <string>  
  2. #include <list>  
  3.   
  4. typdef std::string STATE;  
  5.   
  6.   
  7. class Flyweight  
  8. {  
  9. public:  
  10.     virtual ~Flyweight(){}  
  11.   
  12.     STATE GetInstrinsicState();  
  13.     virtual void Operation(STATE &ExtrinsicState) = 0;  
  14.   
  15. protected:  
  16.     Flyweight(const STATE& state):m_State(state)  
  17.     {  
  18.     }  
  19. private:  
  20.     STATE m_State;  
  21. };  
  22.   
  23.   
  24. class FlyweightFactory  
  25. {  
  26. public:  
  27.     FlyweightFactory(){}  
  28.     ~FlyweightFactory();  
  29.   
  30.     Flyweight* GetFlyweight(const STATE& key);  
  31.   
  32. private:  
  33.     std::list<Flyweight*> m_listFlyweight;  
  34. };  
  35.   
  36.   
  37. class ConcreateFlyweight : public Flyweight  
  38. {  
  39. public:  
  40.     ConcreateFlyweight(const STATE& state) : Flyweight(state)  
  41.     {  
  42.     }  
  43.   
  44.     virtual ~ConcreateFlyweight(){}  
  45.   
  46.     virtual void Operation(STATE &ExtrinsicState);  
  47. };  


(2)Flyweight.cpp

  1. #include "Flyweight.h"  
  2. #include <iostream>  
  3.   
  4.   
  5. inline STATE Flyweight::GetInstrinsicState()  
  6. {  
  7.     return m_State;  
  8. }  
  9.   
  10.   
  11.   
  12. FlyweightFactory::~FlyweightFactory()  
  13. {  
  14.     std::list<Flyweight*>::iterator iter1, iter2, temp;  
  15.   
  16.     for (iter1 = m_listFlyweight.begin();  
  17.          iter2 = m_listFlyweight.end();   
  18.          iter1 != iter2; )  
  19.     {  
  20.         temp = iter1;  
  21.         ++iter1;  
  22.         delete (*temp);  
  23.     }  
  24.   
  25.     m_listFlyweight.clear();  
  26. }  
  27.   
  28.   
  29. Flyweight* FlyweightFactory::GetFlyweight(const STATE &key)  
  30. {  
  31.     std::list<Flyweight*>::iterator iter1, iter2;  
  32.   
  33.     for (iter1 = m_listFlyweight.begin(), iter2 = m_listFlyweight.end();   
  34.         iter1 != iter2;  
  35.         ++iter1)  
  36.     {  
  37.         if ((*iter1)->GetInstrinsicState() == key)  
  38.         {  
  39.             std::cout << "The Flyweight:" << key << "already exists" << std::endl;  
  40.             return (*iter1);  
  41.         }  
  42.     }  
  43.   
  44.     std::cout << "Creating a new Flyweight:" << key << std::endl;  
  45.     Flyweight* flyweight = new ConcreateFlyweight(key);  
  46.     m_listFlyweight.push_back(flyweight);  
  47. }  
  48.   
  49. void ConcreateFlyweight::Operation(STATE & ExtrinsicState)  
  50. {  
  51. }  



(3)main.cpp

  1. #include "FlyWeight.h"  
  2.   
  3. int main()  
  4. {  
  5.     FlyweightFactory flyweightfactory;  
  6.     flyweightfactory.GetFlyweight("Hell");  
  7.     flyweightfactory.GetFlyweight("world");  
  8.     flyweightfactory.GetFlyweight("Hell");  
  9.   
  10.     return 0;  
  11. }  

抱歉!评论已关闭.