设计模式一日一练:亨元模式(Flyweight)
来源:互联网 发布:aamtool Mac 编辑:程序博客网 时间:2024/04/27 15:59
Flyweight模式,运用共享技术有效地支持大量细粒度的对象。在游戏开发中,享元模式的一个典型应用是动态生成位图字体。
class Texture;// flyweightclass Glyph {public: void Display(int x, int y); public: Texture* tex; // 位图纹理 int c; // 字符 int w; // 宽 int h; // 高}// flyweight factoryclass Font { public: Glyph* GetGlyph(int c); private: std::map<int,Glyph*> glyphs;}Glyph* GetGlyph(int c) { Glyph* glyph = null; if (glyphs.Contains(c)) { glyph = glyphs[c]; } else { glyph = new Glyph(); glyph.c = c; // todo ... 创建位图纹理,设置宽高 glyphs[c] = glyph; } return glyph;}// testvoid Test() { const char* str = "aabccc"; Font* font = new Font(); for (char* ptr = str; *ptr != '\0'; ptr++) { Glyph* glyph = font->GetGlyph(*ptr); glyph->Display((ptr - str) * 20, 0); } // todo ... destroy}PS. 我的设计模式系列blog,《设计模式》专栏,通过简单的示例演示设计模式,对于初学者很容易理解入门。深入学习请看GoF的《设计模式》。
13 0
- 设计模式一日一练:亨元模式(Flyweight)
- 设计模式一日一练:单件模式(Singleton)
- 设计模式一日一练:策略模式(Strategy)
- 设计模式一日一练:工厂模式(Factory)
- 设计模式一日一练:适配器模式(Adapter)
- 设计模式一日一练:装饰者模式(Decorator)
- 设计模式一日一练:观察者模式(Observer)
- 设计模式一日一练:代理模式(Proxy)
- 设计模式一日一练:生成器模式(Builder)
- 设计模式一日一练:命令模式(Command)
- 设计模式一日一练:组合模式(Composite)
- 设计模式一日一练:外观模式(Facade)
- 设计模式一日一练:桥接模式(Bridge)
- 设计模式一日一练:原型模式(Prototype)
- 设计模式一日一练:状态模式(State)
- 设计模式一日一练:中介者模式(Mediator)
- 设计模式:享元模式(Flyweight)
- 设计模式:享元模式(Flyweight)
- I/O事件处理模型之Reactor和Proactor
- 设计模式一日一练:原型模式(Prototype)
- 博客网站互访价值在哪里?
- 【翻译】两种高性能I/O设计模式(Reactor/Proactor)的比较
- 高仿遇见3.2.1客户端
- 设计模式一日一练:亨元模式(Flyweight)
- 一个典型的Java程序分析
- Gdiplus::DrawString
- cocos2d+TexturePackerGUI动画制作
- 第三章 引擎的核心:渲染框架
- Android实现数据存储技术
- Android模仿iPhone View旋转刷新数据动画详解
- 目前流行的缺陷管理工具
- typedef的使用