软件模式之Flyweight模式.docx

上传人:scccc 文档编号:14522127 上传时间:2022-02-08 格式:DOCX 页数:14 大小:14.82KB
返回 下载 相关 举报
软件模式之Flyweight模式.docx_第1页
第1页 / 共14页
软件模式之Flyweight模式.docx_第2页
第2页 / 共14页
软件模式之Flyweight模式.docx_第3页
第3页 / 共14页
软件模式之Flyweight模式.docx_第4页
第4页 / 共14页
软件模式之Flyweight模式.docx_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《软件模式之Flyweight模式.docx》由会员分享,可在线阅读,更多相关《软件模式之Flyweight模式.docx(14页珍藏版)》请在三一文库上搜索。

1、Flyweight模式一意图运用共享技术有效地支持大量细粒度的对象。二适用性 1、一个应用程序使用了大量的对象。 2、完全由于使用大量的对象,造成很大的存储开销。 3、对象的大多数状态都可变为外部状态。 4、如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。 5、应用程序不依赖于对象标识。由于Flyweight 对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。三实例:设计一个图书的管理系统,每本书都有出版社、编号、作者、价钱等的属性,所以这个书的类可以这样设计:class Bookpublic: string GetPublish() return m_publ

2、ishCompany; 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_pr

3、ice = price; void SetName(string &s) m_name = s;private: string m_publishCompany; / 出版社 string m_writer; / 作者 int m_bookID; / 书籍编号 int m_price; / 价钱 string m_name; / 书名; 这非常自然。但是随着系统的开发和试运行,这种设计就表现出一些不足的地方了,例如一般图书会非常多,成千上万本,在这些书中可能会有相同的出版社或者相同的作者,那对每本书来说就会重复了,也就是浪费了一些重复的空间。如果每本书都耗费一些空间,那整个软件系统所耗费的空间

4、是不可想象的。所以,我们就要想办法来解决这个问题。想象一下系统里面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

5、+是好劲); book3.SetPublish(机械工业出版社); book3.SetWriter(一雨田); book3.SetBookID(0002); book3.SetPrice(50); book3.SetName(C+无得顶,我是铁头功.); 这里有两个“机械工业出版社”和两个“候捷”,重复了。如果使用共享模式的话,这里的浪费就可以避免了。 别看这里只有两个,想象一下成千上万本书时的空间浪费.2、使用共享模式,把出版社和作者两个属性作为享元(FlyWeight) PublishFlyweightFactory pff; WriterFlyweightFactory wff; Boo

6、k 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(000

7、1); 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和Get

8、Writer的实现: 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的实现大同小异,这里就不列出来了,请看详细代码里的实现。 下

9、面是完整的代码,可以在一个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() re

10、turn 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; / 书籍

11、编号 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.fin

12、d(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 =

13、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

14、() 字节的空间 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.GetWr

15、iter() 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.SetName(string(); ShowBookInfo(book2); book3.SetPublish( &(pff.GetPublish(机械工业出版社)-GetName() ); book3.SetWriter( &(wff.GetWriter(一雨田)-GetName() ); book3.SetBookID(0002); book3.SetPrice(50); book3.SetName(string(); ShowBookInfo(book3);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1