设计模式简单代码之Flyweight模式(二)

设计模式简单代码之Flyweight模式(二)

作者:qmroom

来源:http://blog.csdn.net/qmroom

 

在上篇文章“设计模式简单代码之Flyweight模式”中,我们发现以下两个问题:

  1. 随着使用Flyweight模式的增多,发现需要增加大量的代码,使用不是很方便;
  2. 第二个问题也是最重要的问题,仔细研读代码,发现代码并没有释放字符串内存空间,大量使用会造成内存严重丢失。

解决问题一我们可以使用模板类解决;解决问题二我们需要为每个Flyweight增加一个容器类,并增加析构函数,代码如下:

  1. /*
  2. 作者:qmroom
  3. */
  4. // Flyweight.cpp 
  5. #include <iostream> 
  6. #include <map> 
  7. #include <string> 
  8. #include <process.h> 
  9. using namespace std;
  10. typedef char CHAR;
  11. typedef CHAR *LPSTR
  12. typedef const CHAR *LPCSTR;
  13. class Book
  14. {
  15. public:
  16.     string GetPublish() {return *m_publishCompany;} 
  17.     string GetWriter()  {return *m_writer;}
  18.     int GetBookID()  {return m_bookID;} 
  19.     int GetPrice()  {return m_price;}
  20.     string GetName()    {return *m_name;}
  21.     void SetPublish(string *s) {m_publishCompany = s;}
  22.     void SetWriter(string *s) {m_writer = s;}
  23.     void SetBookID(int id)  {m_bookID = id;}
  24.     void SetPrice(int price) {m_price = price;}
  25.     void SetName(string *s)  {m_name = s;}
  26. private
  27.     string *m_publishCompany;   // 出版社
  28.     string *m_writer;           // 作者
  29.     int m_bookID;               // 书籍编号
  30.     int m_price;                // 价钱
  31.     string *m_name;             // 书名
  32. };
  33. //对象
  34. template<typename T_Value = string>
  35. class FlyweightItem
  36. {
  37. public:
  38.     FlyweightItem(T_Value &s)
  39.     {
  40.         m_value = new T_Value(s);
  41.     }
  42.     virtual ~FlyweightItem()
  43.     {
  44.         delete(m_value);
  45.     }
  46.     inline T_Value *GetValue()
  47.     { 
  48.         return m_value;
  49.     }
  50. private:
  51.     T_Value *m_value;
  52. };
  53. //对象容器
  54. template<typename T_Index=string, typename T_Value=string>
  55. class FlyweightStdMap : public map<T_Index, FlyweightItem<T_Value>*>
  56. {
  57. public:
  58.     virtual ~FlyweightStdMap()
  59.     {
  60.         iterator    itr;
  61.         for (itr = this->begin(); itr != this->end(); itr++)
  62.         {
  63.             // second就是指 map<T_Index, FlyweightItem<T_Value>*>的 FlyweightItem<T_Value>*
  64.             delete (*itr).second;   //释放内存
  65.         }
  66.     }
  67. };
  68. //对象工厂
  69. template<typename T_Index=string, typename T_Value=string>
  70. class FlyweightItemStdFactory
  71. {
  72. public:
  73.     typedef FlyweightItem<T_Value>                          item;
  74.     typedef FlyweightStdMap<T_Index, T_Value>               std_map;
  75. public:
  76.     virtual item* GetItem(T_Index key)
  77.     {
  78.         item *p;
  79.         FlyweightStdMap<T_Index, T_Value>::iterator it; 
  80.         it = mapPublish.find(key);
  81.         //如果存在这个对象 
  82.         if(it != mapPublish.end() ) 
  83.         {
  84.             // second就是指 map<T_Index, FlyweightItem<T_Value>*>的 FlyweightItem<T_Value>*
  85.             p = (*it).second; 
  86.             cout << "已经有这个对象: /"" << *(p->GetValue()) << "/" 你节省了" << strlen((*(p->GetValue())).c_str()) << "字节的空间" << endl;
  87.         } 
  88.         else
  89.         {
  90.             // 插入这个item 
  91.             p = new item(key);
  92.             mapPublish[key] = p;
  93.         }
  94.         return p;
  95.     }
  96.     inline T_Value *GetValue(T_Index key)
  97.     {
  98.         return GetItem(key)->GetValue();
  99.     }
  100. private:
  101.     std_map mapPublish; 
  102. };
  103. typedef FlyweightItemStdFactory<>   FlyweightFactory;
  104. void ShowBookInfo(Book book)
  105.     cout << "书名:" << book.GetName() << endl;
  106.     cout << "编号:" << book.GetBookID() << endl; 
  107.     cout << "价钱:" << book.GetPrice() << endl;
  108.     cout << "出版:" << book.GetPublish() << endl; 
  109.     cout << "作者:" << book.GetWriter() << endl;
  110.     cout << endl; 
  111. }
  112. void main()
  113.     FlyweightFactory        pff;    //出版社工厂
  114.     FlyweightFactory        wff;    //作者工厂
  115.     FlyweightFactory        nff;    //书名工厂
  116.     Book book1, book2, book3;
  117.     book1.SetPublish( pff.GetValue("机械工业出版社"));
  118.     book1.SetWriter( wff.GetValue("qmroom"));
  119.     book1.SetBookID(0000);
  120.     book1.SetPrice(20);
  121.     book1.SetName(nff.GetValue("<<C++好野>>"));
  122.     ShowBookInfo(book1);
  123.     book2.SetPublish( pff.GetValue("人民邮电出版社"));
  124.     book2.SetWriter( wff.GetValue("qmroom"));
  125.     book2.SetBookID(0001);
  126.     book2.SetPrice(30);
  127.     book2.SetName(nff.GetValue("<<C++是好劲>>"));
  128.     ShowBookInfo(book2);
  129.     book3.SetPublish( pff.GetValue("机械工业出版社"));
  130.     book3.SetWriter( wff.GetValue("cqm"));
  131.     book3.SetBookID(0002);
  132.     book3.SetPrice(50);
  133.     book3.SetName(nff.GetValue("<<C++无得顶,我是铁头功...>>"));
  134.     ShowBookInfo(book3);
  135.     system("pause");

        好了,至此可以看出,利用上面的代码已经可以很好的工作。但最好还是动手把代码拷贝到VC 里,编译运行一下,在释放内存的地方加上断点,便于深刻理解Flyweight模式。

 

上一篇

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值