[设计模式] —— Flyweight 享元模式

Flyweight 享元模式

属于对象性能模式,主要考虑面向对象所带来的成本问题。

动机

软件系统采用纯对象方案的问题在于大量细粒度的对象会很快充斥整个系统,从而带来很高的运行时代价。

如何避免大量细粒度对象问题的同时,让外部客户程序依然能够透明地使用面向对象的方式来进行操作?

定义

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

示例代码
class Font {
private:
 string key;

 // ...

public:
 Font(const string& key) {
     // ...
 }
};

class FontFactory {
private:
 map<string, Font*> fontPool;

public:
 Font* GetFont(const string& key) {
     map<string, Font*>::iterator itr = fontPool.find(key);
     if(itr != fontPool.end()) {
         return fontPool[key];
     } else {
         Font* font = new Font(key);
         fontPool[key] = font;
         return font;
     }
 }
};

我们会有一个数据结构存储现在已经产生的细粒度对象,需要根据关键字来索引,当有相同关键字的对象时,就直接返回。当发现相关 key 对象还没有创建时,则新建对象返回,并将新建的对象加入到存储队列当中,下次查找就会命中。

结构图
总结
  • Flyweight 主要考虑的是面向对象的代价问题,一般不涉及到面向对象的抽象性问题
  • Flyweight 采用对象共享的做法来降低系统中对象的个数,从而降低面向对象带来的内存压力

其他设计模式汇总:
[设计模式] —— 设计模式的介绍及分类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计模式精解-GoF 23 种设计模式解析附 C++实现源码 目 录 0 引言 ..............................................................................................................................................2 0.1 设计模式解析(总序).....................................................................................................2 0.2 设计模式解析后记.............................................................................................................2 0.3 与作者联系........................................................................................................................5 1 创建型模式...................................................................................................................................5 1.1 Factory模式 ........................................................................................................................5 1.2 AbstactFactory模式 ..........................................................................................................11 1.3 Singleton模式 ...................................................................................................................19 1.4 Builder模式.......................................................................................................................22 1.5 Prototype模式...................................................................................................................29 2 结构型模式.................................................................................................................................33 2.1 Bridge模式........................................................................................................................33 2.2 Adapter模式......................................................................................................................39 2.3 Decorator模式...................................................................................................................47 2.4 Composite模式 .................................................................................................................52 2.5 Flyweight模式 ..................................................................................................................58 2.6 Facade模式 .......................................................................................................................64 2.7 Proxy模式.........................................................................................................................70 3 行为模式.....................................................................................................................................74 3.1 Template模式....................................................................................................................74 3.2 Strategy模式 .....................................................................................................................80 3.3 State模式...........................................................................................................................87 3.4 Observer模式....................................................................................................................94 3.5 Memento模式 .................................................................................................................103 3.6 Mediator模式..................................................................................................................108 3.7 Command模式................................................................................................................117 3.8 Visitor模式......................................................................................................................126 3.9 Chain of Responsibility模式...........................................................................................136 3.10 Iterator模式...................................................................................................................141 3.11 Interpreter模式..............................................................................................................148 4 在开发中体验设计模式............................................................................................................153 4.1 在开发中体验设计模式.................................................................................................153 4.2 深入理解State模式附C++实现源码..............................................................................155 4.3 也谈double dispatch(双分派)::Visitor 模式..................................................................162 4.4 为什么使用设计模式——从Singleton模式谈起..........................................................164
创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern) 17. 解释器模式(Interpreter Pattern) 18. 中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern) 工程结构 ├─01.Singleton │ ├─html │ └─MySingleton │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─02.ChainOfResponsibility │ ├─html │ ├─My2ChainOfResponsibility │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─MyChainOfResponsibility │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ ├─Refactor │ │ └─TempPE │ └─Properties ├─03.FactoryMethodMode │ ├─FactoryMethodMode │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─04.AbstractFactory │ ├─04.1.SimpleFactory │ │ ├─html │ │ └─SimpleFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─AbstractFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─05.BuilderPattern │ ├─html │ └─MyBuilderPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─06.PrototypePattern │ ├─html │ │ └─C#设计模式(6)——原型模式(Prototype Patt O技术博客_files │ └─PrototypePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─07.AdapterPattern │ ├─html │ └─MyAdapterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─08.BridgePattern │ ├─html │ └─MyBridgePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─09.DecoratorPattern │ ├─html │ └─MyDecoratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─10.CompositePattern │ ├─html │ └─MyCompositePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─11.FacadePattern │ ├─html │ └─MyFacadePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─12.FlyweightPattern │ ├─html │ └─MyFlyweightPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─13.ProxyPattern │ ├─html │ └─MyProxyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─14.TemplateMethod │ ├─html │ └─MyTemplateMethod │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─15.VisitorPattern │ ├─html │ └─MyVisitorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─16.StrategyPattern │ ├─html │ └─MyStrategyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─17.StatePattern │ ├─html │ └─StatePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─18.MementoPattern │ ├─html │ └─MementoPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─19.MediatorPattern │ ├─html │ └─MediatorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─20.OberverPattern │ ├─CatOberverPattern │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─html │ └─MyOberverPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─21.IteratorPattern │ ├─html │ └─IteratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─22.InterpreterPattern │ ├─html │ └─MyInterpreterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties └─23.CommandPattern ├─html └─MyCommandPattern ├─bin │ └─Debug ├─obj │ └─Debug │ └─TempPE └─Properties
书名: 设计模式可复用面向对象软件的基础 英文原书名: Design Patterns:Elements of Reusable Object-Oriented software 作者: Erich Gamma 等 译者: 李英军 马晓星 蔡敏 刘建中 书号: 7-111-07575-7 页码: 254 定价: ¥35.00 会员价: ¥31.50 币值: 315 出版日期: 2000-9-1 本书设计实例从面向对象的设计中精选出23个设计模式,总结了面向对象设计中最有价值的经验,并且用简洁可复用的形式表达出来。本书分类描述了一组设计良好,表达清楚的软件设计模式,这些模式在实用环境下有特别有用。 -------- 目 录 序言 前言 读者指南 第1章 引言 1 1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象接口 9 1.6.4 描述对象的实现 10 1.6.5 运用复用机制 13 1.6.6 关联运行时刻和编译时刻的 结构 15 1.6.7 设计应支持变化 16 1.7 怎样选择设计模式 19 1.8 怎样使用设计模式 20 第2章 实例研究:设计一个文档编 辑器 22 2.1 设计问题 23 2.2 文档结构 23 2.2.1 递归组合 24 2.2.2 图元 25 2.2.3 组合模式 27 2.3 格式化 27 2.3.1 封装格式化算法 27 2.3.2 Compositor和Composition 27 2.3.3 策略模式 29 2.4 修饰用户界面 29 2.4.1 透明围栏 29 2.4.2 Monoglyph 30 2.4.3 Decorator 模式 32 2.5 支持多种视感标准 32 2.5.1 对象创建的抽象 32 2.5.2 工厂类和产品类 33 2.5.3 Abstract Factory模式 35 2.6 支持多种窗口系统 35 2.6.1 我们是否可以使用Abstract Factory 模式 35 2.6.2 封装实现依赖关系 35 2.6.3 Window和WindowImp 37 2.6.4 Bridge 模式 40 2.7 用户操作 40 2.7.1 封装一个请求 41 2.7.2 Command 类及其子类 41 2.7.3 撤消和重做 42 2.7.4 命令历史记录 42 2.7.5 Command 模式 44 2.8 拼写检查和断字处理 44 2.8.1 访问分散的信息 44 2.8.2 封装访问和遍历 45 2.8.3 Iterator类及其子类 46 2.8.4 Iterator模式 48 2.8.5 遍历和遍历过程中的动作 48 2.8.6 封装分析 48 2.8.7 Visitor 类及其子类 51 2.8.8 Visitor 模式 52 2.9 小结 53 第3章 创建型模式 54 3.1 Abstract Factory(抽象工厂)— 对象创建型模式 57 3.2 Builder(生成器)—对象创建型 模式 63 3.3 Factory Method(工厂方法)— 对象创建型模式 70 3.4 Prototype(原型)—对象创建型 模式 87 3.5 Singleton(单件)—对象创建型 模式 84 3.6 创建型模式的讨论 89 第4章 结构型模式 91 4.1 Adapter(适配器)—类对象结构型 模式 92 4.2 Bridge(桥接)—对象结构型 模式 100 4.3 Composite(组成)—对象结构型 模式 107 4.4 Decorator(装饰)—对象结构型 模式 115 4.5 FACADE(外观)—对象结构型 模式 121 4.6 Flyweight(享元)—对象结构型 模式 128 4.7 Proxy(代理)—对象结构型 模式 137 4.8 结构型模式的讨论 144 4.8.1 Adapter与Bridge 144 4.8.2 Composite、Decorator与Proxy 145 第5章 行为模式 147 5.1 CHAIN OF RESPONSIBIL ITY(职责链) —对象行为型模式 147 5.2 COMMAND(命令)—对象行为型 模式 154 5.3 INTERPRETER(解释器)—类行为型 模式 162 5.4 ITERATOR(迭代器)—对象行为型 模式 171 5.5 MEDIATOR(中介者)—对象行为型 模式 181 5.6 MEMENTO(备忘录)—对象行为型 模式 188 5.7 OBSERVER(观察者)—对象行为型 模式 194 5.8 STATE(状态)—对象行为型模式 201 5.9 STRATEGY(策略)—对象行为型 模式 208 5.10 TEMPLATE METHOD(模板方法) —类行为型模式 214 5.11 VISITOR(访问者)—对象行为型 模式 218 5.12 行为模式的讨论 228 5.12 1 封装变化 228 5.12.2 对象作为参数 228 5.12.3 通信应该被封装还是被分布 229 5.12.4 对发送者和接收者解耦 229 5.12.5 总结 231 第6章 结论 232 6.1 设计模式将带来什么 232 6.2 一套通用的设计词汇 232 6.3 书写文档和学习的辅助手段 232 6.4 现有方法的一种补充 233 6.5 重构的目标 233 6.6 本书简史 234 6.7 模式界 235 6.8 Alexander 的模式语言 235 6.9 软件中的模式 236 6.10 邀请参与 237 6.11 临别感想 237 附录A 词汇表 238 附录B 图示符号指南 241 附录C 基本类 244 参考文献 249

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值