设计模式之享元模式

1、享元模式运用共享技术有效地支持大量细粒度的对象。

UML图如下:


2、思考:Flyweight根据客户需求返回已经生成好的对象,但一定要事先生成对象实例吗?

答:实际上是不一定需要的,完全可以初始化的时候什么也不做,到需要的时候,再去判断对象是否为NULL来决定是否实例化。


3、思考:为什么要有UnsharedConcreteFlyweight的存在呢?

答:因为尽管我们大部分时间都需要共享对象来降低内存的消耗,但个别时候也有可能不需要共享的,那么此时的UnsharedConcreteFlyweight子类就有存在的必要了,它可以解决那些不需要共享对象的问题。


4、内部状态和外部状态

在享元对象内部并且不会随环境改变而改变的共享部分,可以称为是享元对象的内部状态;而随着环境变化而变化的不可共享的状态就是说外部状态了。

事实上,享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,有时就能够大幅度地减小需要实例化的类的数量。如果能把那些参数移到类实例的外面,在方法调用时将它们传递过来,就可以通过共享大幅度地减少单个实例的数目。

也就是说,享元模式Flyweight执行时所需的状态是有内部的也有可能有外部状态的,内部状态存储在ConcreteFlyweight中,而对象的外部状态考虑有客户端对象存储或计算,当调用Flyweight对象的操作时,将该状态传递给它。


5、享元模式的应用:什么时候使用享元模式?

答:如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就应该考虑使用;还有就是对象的大多数状态可以是外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象,此时可以考虑使用享元模式。

  1. #include <iostream>  
  2. #include <cstring>  
  3. using namespace std;  

  4. /* Flyweight类,它是所有具体享元类的 
  5.     超类或接口,通过这个接口,Flyweight 
  6.     可以接受并作用于外部状态 */  
  7. class Flyweight  
  8. {  
  9. public:  
  10.     char name[10];  
  11.     virtual void Operation(int) = 0;  
  12. };  
  13. /*  ConcreteFlyweight是继承了Flyweight超类或实现 
  14.     Flyweight接口,并为内部状态增加存储空间 */  
  15. class ConcreteFlyweight : public Flyweight  
  16. {  
  17. public:  
  18.     void Operation(int extrinsicstate)   
  19.     {  
  20.         cout <<"具体Flyweight_" << name <<":" << extrinsicstate << endl;  
  21.     }  
  22. };  
  23. /*  UnsharedConcreteFlyweight是指那些不需要共享的 
  24.     Flyweight子类。因为Flyweight接口共享成为可能 
  25.     但它不强制共享 */  
  26. class UnsharedConcreteFlyweight : public Flyweight  
  27. {  
  28. public:  
  29.     UnsharedConcreteFlyweight(char * str)   
  30.     {  
  31.         strcpy(name, str);  
  32.     }  
  33.       
  34.     void Operation(int extrinsicstate)   
  35.     {  
  36.         cout << "不共享的具体Flyweight_" << name <<":" << extrinsicstate << endl;  
  37.     }  
  38. };  
  39. /*  node 节点 */  
  40. struct node   
  41. {  
  42.     node * next[26];  
  43.     Flyweight * pFlyweight;  
  44. };  
  45. /*  自己写的伪map */  
  46. class MyMap  
  47. {  
  48. private:  
  49.     node * root;  
  50. public:  
  51.     MyMap();  
  52.     node * getNode();  
  53.     void insert(char * str, Flyweight * pFlyweight);  
  54.     Flyweight * query(char * str);  
  55. };  
  56. node * MyMap::getNode() {  
  57.     node * pNode = (node *)malloc(sizeof(node));  
  58.     pNode->pFlyweight = NULL;  
  59.     memset(pNode->next, 0, sizeof(pNode->next));  
  60.     return pNode;  
  61. }  
  62. MyMap::MyMap()   
  63. {  
  64.     root = getNode();  
  65. }  
  66. void MyMap::insert(char * str, Flyweight * pFlyweight)   
  67. {  
  68.     node * pCur = root;  
  69.     int len = strlen(str);  
  70.     for (int i = 0; i < len; ++i)   
  71.     {  
  72.         int index = str[i] - 'a';  
  73.         if (pCur->next[index] == NULL)   
  74.         {  
  75.             pCur->next[index] = getNode();  
  76.         }  
  77.         pCur = pCur->next[index];  
  78.     }  
  79.     pCur->pFlyweight = pFlyweight;  
  80.     strcpy(pCur->pFlyweight->name, str);  
  81. }  
  82. Flyweight * MyMap::query(char * str)   
  83. {  
  84.     node * pCur = root;  
  85.     int len = strlen(str);  
  86.     for (int i = 0; i < len; ++i)  
  87.     {  
  88.         int index = str[i] - 'a';  
  89.         if (pCur->next[index] == NULL)   
  90.         {  
  91.             return NULL;  
  92.         }   
  93.         pCur = pCur->next[index];  
  94.     }  
  95.     return pCur->pFlyweight;  
  96. }  
  97. /* 
  98.     FlyweightFactory是一个享元工厂,用来创建并 
  99.     管理Flyweight对象,它主要是用来确保合理地 
  100.     共享Flyweight对象提供一个已创建的实例或者 
  101.     创建一个(如果不存在) 
  102.  */  
  103. class FlyweightFactory  
  104. {  
  105. private:  
  106.     MyMap map;  
  107. public:  
  108.     FlyweightFactory()   
  109.     {  
  110.         map.insert("x"new ConcreteFlyweight());  
  111.         map.insert("y"new ConcreteFlyweight());  
  112.         map.insert("z"new ConcreteFlyweight());  
  113.     }  
  114.       
  115.     Flyweight * getFlyweight(char * str)   
  116.     {  
  117.         return map.query(str);  
  118.     }  
  119. };  
  120. int main()  
  121. {  
  122.     //代码外部状态  
  123.     int extrinsicstate = 22;  
  124.     FlyweightFactory factory;  
  125.     Flyweight * pFx = factory.getFlyweight("x");  
  126.     pFx->Operation(--extrinsicstate);  
  127.     Flyweight * pFy= factory.getFlyweight("y");  
  128.     pFy->Operation(--extrinsicstate);  
  129.     Flyweight * pFz = factory.getFlyweight("z");  
  130.     pFz->Operation(--extrinsicstate);  
  131.     Flyweight * pUnsharedF = new UnsharedConcreteFlyweight("u");  
  132.     pUnsharedF->Operation(--extrinsicstate);  
  133.     return 0;  
  134. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值