设计模式读书笔记:Flyweight(享元)
2015-07-10 17:17
465 查看
意图:
运用共享技术有效地支持大量细粒度的对象。
结构图:来自 《23种设计模式 - 郗晓勇》
实现:https://github.com/panshiqu/patterns/tree/master/Flyweight
FlyweightFactory
运用共享技术有效地支持大量细粒度的对象。
结构图:来自 《23种设计模式 - 郗晓勇》
实现:https://github.com/panshiqu/patterns/tree/master/Flyweight
FlyweightFactory
#include "ConcreteFlyweight.h" #include <map> namespace NS_FLYWEIGHT { class FlyweightFactory { public: FlyweightFactory() {} virtual ~FlyweightFactory() { std::map<int, Flyweight *>::iterator itr = _flyweights.begin(); for (; itr != _flyweights.end(); itr++) delete itr->second; _flyweights.clear(); } Flyweight *getFlyweight(int key) { std::map<int, Flyweight *>::iterator itr = _flyweights.find(key); if (itr != _flyweights.end()) return itr->second; ConcreteFlyweight *flyweight = new ConcreteFlyweight(); flyweight->setIntrinsicState(key); _flyweights.insert(std::make_pair(key, flyweight)); return flyweight; } private: std::map<int, Flyweight *> _flyweights; }; } /* namespace NS_FLYWEIGHT */Flyweight
#include <iostream> namespace NS_FLYWEIGHT { class Flyweight { public: Flyweight() {} virtual ~Flyweight() {} virtual void operation(std::string extrinsicState) = 0; }; } /* namespace NS_FLYWEIGHT */ConcreteFlyweight
#include "Flyweight.h" namespace NS_FLYWEIGHT { class ConcreteFlyweight : public Flyweight { public: ConcreteFlyweight() : _intrinsicState(0) {} virtual ~ConcreteFlyweight() {} virtual void operation(std::string extrinsicState) { std::cout << extrinsicState << " ConcreteFlyweight - " << _intrinsicState << std::endl; } void setIntrinsicState(int intrinsicState) { _intrinsicState = intrinsicState; } private: int _intrinsicState; }; } /* namespace NS_FLYWEIGHT */UnsharedConcreteFlyweight
#include "Flyweight.h" #include <map> namespace NS_FLYWEIGHT { class UnsharedConcreteFlyweight : public Flyweight { public: UnsharedConcreteFlyweight() {} virtual ~UnsharedConcreteFlyweight() {} virtual void operation(std::string extrinsicState) { std::multimap<Flyweight *, std::string>::iterator itr = _flyweights.begin(); for (; itr != _flyweights.end(); itr++) { std::string str = itr->second; if (str == "") str = extrinsicState; itr->first->operation(str); } } virtual void add(Flyweight *flyweight, std::string extrinsicState) { _flyweights.insert(make_pair(flyweight, extrinsicState)); } virtual void remove(Flyweight *flyweight) { // do remove } private: std::multimap<Flyweight *, std::string> _flyweights; }; } /* namespace NS_FLYWEIGHT */main
#include "Flyweight/FlyweightFactory.h" #include "Flyweight/UnsharedConcreteFlyweight.h" using namespace NS_FLYWEIGHT; int main(void) { FlyweightFactory ff; UnsharedConcreteFlyweight ucf; Flyweight *f1 = ff.getFlyweight(1); ucf.add(f1, ""); Flyweight *f2 = ff.getFlyweight(1); ucf.add(f2, "black"); ucf.operation("red"); }附加:
相关文章推荐
- Oracle 查询优化器 -- 改写查询语句
- iOS 系统消息通知
- C++重载运算符
- WCF技术剖析之三十:一个很有用的WCF调用编程技巧[下篇]
- php使用PDO,并连接SQL
- typedef和define的详细区别
- [leedcode 41] First Missing Positive
- 一位运营该做的事情
- Android 获取手机中的图片信息的两种方法
- binder驱动-------之内存映射篇
- java list 交集 并集 差集 去重复并集
- 偷懒代码块之保持屏幕常亮
- 关于 NSURL 的解析和编码
- 使用gdb调试程序详解
- SVN去掉已保存的密码
- node.js的npm安装
- 自己写的Android端HttpUtil工具类
- Innosetup打包自动下载.net framework 动态库及替换卸载程序图标.
- [JAVA] 冻结Excel的第一行或第一列
- C语言中的强符号与弱符号