软件模式之Flyweight模式.doc_第1页
软件模式之Flyweight模式.doc_第2页
软件模式之Flyweight模式.doc_第3页
软件模式之Flyweight模式.doc_第4页
软件模式之Flyweight模式.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Flyweight模式一意图运用共享技术有效地支持大量细粒度的对象。二适用性 1、一个应用程序使用了大量的对象。 2、完全由于使用大量的对象,造成很大的存储开销。 3、对象的大多数状态都可变为外部状态。 4、如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。 5、应用程序不依赖于对象标识。由于Flyweight 对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。三实例:设计一个图书的管理系统,每本书都有出版社、编号、作者、价钱等的属性,所以这个书的类可以这样设计:class Bookpublic: string GetPublish() return m_publishCompany; string GetWriter() return m_writer; int GetBookID() return m_bookID; int GetPrice() return m_price; string GetName() return m_name; void SetPublish(string &s) m_publishCompany = s; void SetWriter(string &s) m_writer = s; void SetBookID(int id) m_bookID = id; void SetPrice(int price) m_price = price; void SetName(string &s) m_name = s;private: string m_publishCompany; / 出版社 string m_writer; / 作者 int m_bookID; / 书籍编号 int m_price; / 价钱 string m_name; / 书名; 这非常自然。但是随着系统的开发和试运行,这种设计就表现出一些不足的地方了,例如一般图书会非常多,成千上万本,在这些书中可能会有相同的出版社或者相同的作者,那对每本书来说就会重复了,也就是浪费了一些重复的空间。如果每本书都耗费一些空间,那整个软件系统所耗费的空间是不可想象的。所以,我们就要想办法来解决这个问题。想象一下系统里面Book类的使用: 1、先看看不用共享模式: Book book1, book2, book3; book1.SetPublish(机械工业出版社); book1.SetWriter(候捷); book1.SetBookID(0000); book1.SetPrice(20); book1.SetName(C+好野); book2.SetPublish(人民邮电出版社); book2.SetWriter(候捷); book2.SetBookID(0001); book2.SetPrice(30); book2.SetName(C+是好劲); book3.SetPublish(机械工业出版社); book3.SetWriter(一雨田); book3.SetBookID(0002); book3.SetPrice(50); book3.SetName(C+无得顶,我是铁头功.); 这里有两个“机械工业出版社”和两个“候捷”,重复了。如果使用共享模式的话,这里的浪费就可以避免了。 别看这里只有两个,想象一下成千上万本书时的空间浪费.2、使用共享模式,把出版社和作者两个属性作为享元(FlyWeight) PublishFlyweightFactory pff; WriterFlyweightFactory wff; Book book1, book2, book3; book1.SetPublish(pff.GetPublish(机械工业出版社)-GetName(); book1.SetWriter(wff.GetWriter(候捷)-GetName(); book1.SetBookID(0000); book1.SetPrice(20); book1.SetName(C+好野); book2.SetPublish(pff.GetPublish(人民邮电出版社)-GetName(); book2.SetWriter(wff.GetWriter(候捷)-GetName(); book2.SetBookID(0001); book2.SetPrice(30); book2.SetName(C+是好劲); book3.SetPublish(pff.GetPublish(机械工业出版社)-GetName(); book3.SetWriter(wff.GetWriter(一雨田)-GetName(); book3.SetBookID(0002); book3.SetPrice(50); book3.SetName(C+无得顶,我是铁头功.); 为什么使用了PublishFlyweightFactory和WriterFlyweightFactory之后就可以节省空间了呢? 奥妙就在于GetPublish和GetWriter的实现: PublishFlyweight* GetPublish(string key) PublishFlyweight *p; mapPublish:iterator it; it = mapPublish.find(key); / 存在这个Writer if(it != mapPublish.end() ) p = it; else / 插入这个PublishFlyweight p = new PublishFlyweight(key); mapPublishkey = p; return p; GetWriter的实现大同小异,这里就不列出来了,请看详细代码里的实现。 下面是完整的代码,可以在一个CPP文件里编译使用:/ Flyweight.cpp#pragma warning(disable: 4786)#include #include #include using namespace std;class Bookpublic: string GetPublish() return *m_publishCompany; string GetWriter() return *m_writer; int GetBookID() return m_bookID; int GetPrice() return m_price; string GetName() return m_name; void SetPublish(string *s) m_publishCompany = s; void SetWriter(string *s) m_writer = s; void SetBookID(int id) m_bookID = id; void SetPrice(int price) m_price = price; void SetName(string &s) m_name = s; private: string *m_publishCompany; / 出版社 string *m_writer; / 作者 int m_bookID; / 书籍编号 int m_price; / 价钱 string m_name; / 书名;class PublishFlyweightpublic: PublishFlyweight(string s) m_name = s; string GetName() return m_name; private: string m_name;class PublishFlyweightFactorypublic: PublishFlyweight* GetPublish(string key) PublishFlyweight *p; map:iterator it; it = mapPublish.find(key); / 存在这个出版社 if(it != mapPublish.end() ) p = (*it).second; cout 已经有这个出版社: GetName() 你节省了 GetName().c_str() 字节的空间 endl; else / 插入这个PublishFlyweight p = new PublishFlyweight(key); mapPublishkey = p; return p; private: map mapPublish;class WriterFlyweightpublic: WriterFlyweight(string s) m_name = s; string GetName() return m_name; private: string m_name;class WriterFlyweightFactorypublic: WriterFlyweight* GetWriter(string key) WriterFlyweight *p; map:iterator it; it = mapWriter.find(key); / 存在这个Writer if(it != mapWriter.end() ) p = (*it).second; cout 已经有这个作者名字: GetName() 你节省了 GetName().c_str() 字节的空间 endl; else / 插入这个PublishFlyweight p = new WriterFlyweight(key); mapWriterkey = p; return p; private: map mapWriter; ;void ShowBookInfo(Book book) cout 书名: book.GetName() endl; cout 编号: book.GetBookID() endl; cout 价钱: book.GetPrice() endl; cout 出版: book.GetPublish() endl; cout 作者: book.GetWriter() endl; cout GetName() ); book1.SetWriter( &(wff.GetWriter(候捷)-GetName() ); book1.SetBookID(0000); book1.SetPrice(20); book1.SetName(string(); ShowBookInfo(book1); book2.SetPublish( &(pff.GetPublish(人民邮电出版社)-GetName() ); book2.SetWriter( &(wff.GetWriter(候捷)-GetName() ); book2.SetBookID(0001); book2.SetPrice(30); book2.Set

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论