设计模式--结构型模式之六-Flyweight享元

 

意图:

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

适用性:

1一个应用程序使用了大量的对象

2由于使用大量的对象造成很大的开销

3对象的大多数状态都可变为外部状态

4如果删除对象的外部状态,可以用相对较少的共享对象,取代很多组对象

5应用程序不依赖于对象标识

 

 

效果:

使用FLYWEIGHT模式后,传输查找和计算外部状态都会产生开销,然而空间上的节省抵消了这些开销。

1因为共享,实例总数减少的数目

2对象内部状态的平均数目

3外部状态存储

 

实现:

1删除外部状态 外部状态有一个单独的对象结构计算得到,且该结构的存储要求非常小

2管理共享对象 对象共享,用户不能直接实例化。FlyWeight可以帮助用户查找某个特定的FlyWeight对象,若不存在,则创建一个FlyWeight。还包括引用计数和垃圾回收

Flyweight.h

  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4. const int MAX_GLYPH_NUM = 128;
  5. class GlyphContext
  6. {
  7. public:
  8.     GlyphContext(){size=1;font=1;}
  9.     virtual ~GlyphContext(){size=0;font=0;};
  10.     int size;
  11.     int font;
  12. };
  13. class Glyph
  14. {
  15. public:
  16.    Glyph()
  17.     {cout<<"Glyph is created"<<endl;
  18.     m_content="0";
  19.     }
  20.     virtual ~Glyph(){cout<<"Glyph is destoryed"<<endl;}
  21.     virtual void Draw(GlyphContext& ct)
  22.     {cout<<"Draw  "<<m_content<<" with size="<<ct.size<<",font="<<ct.font<<endl;}
  23.     virtual string GetContent(){return m_content;}
  24.     virtual void  SetContent(string s){m_content=s;}
  25. private:
  26.     string m_content;
  27. };
  28. class GlyphFactory
  29. {
  30. public:
  31.     GlyphFactory();
  32.     virtual ~GlyphFactory();
  33.     virtual Glyph* CreateString(string s);
  34. private:
  35.     Glyph*  m_listGlyph[MAX_GLYPH_NUM];
  36. };

Flyweight.cpp

  1. #include "FlyWeight.h"
  2. Glyph* GlyphFactory::CreateString(string s)
  3. {
  4.     bool  all_null = true;
  5.     int i;
  6.     for (i=0;(i<MAX_GLYPH_NUM)&&(m_listGlyph[i]!= 0);i++)
  7.     {
  8.         
  9.         if ( m_listGlyph[i]->GetContent() == s)
  10.         {
  11.             cout<<s<<" has already exists"<<endl;
  12.              return m_listGlyph[i];
  13.         }
  14.     }
  15.     if (i < MAX_GLYPH_NUM)
  16.     {
  17.      m_listGlyph[i] = new Glyph;
  18.      m_listGlyph[i]->SetContent(s);
  19.         cout<<s<<" is new added"<<endl;
  20.      return m_listGlyph[i];
  21.     }
  22. }
  23. GlyphFactory::GlyphFactory()
  24. {
  25.     for (int i=0;i<MAX_GLYPH_NUM;i++)
  26.     {
  27.         
  28.       m_listGlyph[i]=0;
  29.     }
  30. }
  31. GlyphFactory::~GlyphFactory()
  32. {
  33.     for (int i=0;(i<MAX_GLYPH_NUM)&&(m_listGlyph[i]!= 0);i++)
  34.     {
  35.       delete m_listGlyph[i];
  36.       m_listGlyph[i]=0;
  37.     }
  38. }

 

main.cpp

  1. #include <string> 
  2. #include <iostream> 
  3. using namespace std; 
  4. #include "FlyWeight.h" 
  5. int main()
  6. {GlyphFactory a;
  7.  Glyph* b;
  8.  GlyphContext c;
  9.  b =a.CreateString("Hello");
  10.  b->Draw(c);
  11.  b=a.CreateString("World");
  12.  b->Draw(c);
  13.  b = a.CreateString("Hello");
  14.  b->Draw(c);
  15.   return 0;
  16. }

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值