设计模式之策略模式(Strategy)
2016-03-13 15:08
721 查看
参考《设计模式:可复用面向对象软件的基础》、《大话设计模式》以及百度
策略模式属于对象行为模式中的一种。
意图: 定义一系列的算法,把它们一个个封装起来,并且使他们可相互替换。策略模式使得算法可独立于使用它的客户而变化。
以缓冲算法为例,缓冲算法有:
LFU:最近不经常使用算法。 Least frequently used replace algorithm
LRU:最近最少使用算法。 Least recently used replace algorithm
ARC:自适应缓存替换算法。Automatic Resource Compilation
MRU:最近最常使用算法。 Most commonly recently used algorithm
UML图网上找的一个类似的图,后面自己会重新画一个UML图
替换算法定义
方法一 通过传入一个指针参数,调用指定的算法。暴露了太多的细节
方法二 通过一个参数,此时是一个标签指定一个算法。此时客户只需要知道算法相应的标签,而不需要知道算法。下面有简单工厂模式和策略模式结合起来使用
方法三 利用模板来实现,通过模板参数指定调用的算法
整个代码 //代码只是过程。关键是理解思想,学会画UML类图
策略模式属于对象行为模式中的一种。
意图: 定义一系列的算法,把它们一个个封装起来,并且使他们可相互替换。策略模式使得算法可独立于使用它的客户而变化。
以缓冲算法为例,缓冲算法有:
LFU:最近不经常使用算法。 Least frequently used replace algorithm
LRU:最近最少使用算法。 Least recently used replace algorithm
ARC:自适应缓存替换算法。Automatic Resource Compilation
MRU:最近最常使用算法。 Most commonly recently used algorithm
UML图网上找的一个类似的图,后面自己会重新画一个UML图
替换算法定义
class ReplaceAlgorithm { public: virtual void Replace() = 0; }; //四种具体的替换算法 class LFU_ReplaceAlgorithm : public ReplaceAlgorithm { public: void Replace() { cout << "Least frequently used replace algorithm" << endl; } }; class LRU_ReplaceAlgorithm : public ReplaceAlgorithm { public: void Replace() { cout << "Least recently used replace algorithm" << endl; } }; class ARU_ReplaceAlgorithm : public ReplaceAlgorithm { public: void Replace() { cout << "Automatic Resource Compilation" << endl; } }; class MRU_ReplaceAlgorithm : public ReplaceAlgorithm { public: void Replace() { cout << "Most commonly recently used algorithm" << endl; } };
方法一 通过传入一个指针参数,调用指定的算法。暴露了太多的细节
class Cache { private: ReplaceAlgorithm *m_ra; public: Cache(ReplaceAlgorithm *ra) { m_ra = ra; } ~Cache() { delete m_ra; } void Replace() { m_ra->Replace(); } }; int main() { Cache cache(new LRU_ReplaceAlgorithm()); //暴露了算法的定义 cache.Replace(); return 0; }
方法二 通过一个参数,此时是一个标签指定一个算法。此时客户只需要知道算法相应的标签,而不需要知道算法。下面有简单工厂模式和策略模式结合起来使用
enum RA { LFU, LRU, ARU, MRU }; //标签 class Cache { private: ReplaceAlgorithm *m_ra; public: Cache(enum RA ra) { if (ra == LFU) m_ra = new LFU_ReplaceAlgorithm(); else if (ra == LRU) m_ra = new LRU_ReplaceAlgorithm(); else if (ra == ARU) m_ra = new ARU_ReplaceAlgorithm(); else if (ra == MRU) m_ra = new MRU_ReplaceAlgorithm(); else m_ra = NULL; } ~Cache() { delete m_ra; } void Replace() { m_ra->Replace(); } }; int main() { Cache cache(LRU); //指定标签即可 cache.Replace(); return 0; }
方法三 利用模板来实现,通过模板参数指定调用的算法
template <class RA> class Cache { private: RA m_ra; public: Cache() { } ~Cache() { } void Replace() { m_ra.Replace(); } }; int main() { Cache<LFU_ReplaceAlgorithm> cache; //模板实参 cache.Replace(); return 0; }
整个代码 //代码只是过程。关键是理解思想,学会画UML类图
#include <iostream>
using namespace std;
class ReplaceAlgorithm { public: virtual void Replace() = 0; }; //四种具体的替换算法 class LFU_ReplaceAlgorithm : public ReplaceAlgorithm { public: void Replace() { cout << "Least frequently used replace algorithm" << endl; } }; class LRU_ReplaceAlgorithm : public ReplaceAlgorithm { public: void Replace() { cout << "Least recently used replace algorithm" << endl; } }; class ARU_ReplaceAlgorithm : public ReplaceAlgorithm { public: void Replace() { cout << "Automatic Resource Compilation" << endl; } }; class MRU_ReplaceAlgorithm : public ReplaceAlgorithm { public: void Replace() { cout << "Most commonly recently used algorithm" << endl; } };
/*
//Cache需要用到替换算法
class Cache { private: ReplaceAlgorithm *m_ra; public: Cache(ReplaceAlgorithm *ra) { m_ra = ra; } ~Cache() { delete m_ra; } void Replace() { m_ra->Replace(); } }; int main() { Cache cache(new LRU_ReplaceAlgorithm()); //暴露了算法的定义 cache.Replace(); return 0; }
*/
/*
//Cache需要用到替换算法
enum RA { LFU, LRU, ARU, MRU }; //标签 class Cache { private: ReplaceAlgorithm *m_ra; public: Cache(enum RA ra) { if (ra == LFU) m_ra = new LFU_ReplaceAlgorithm(); else if (ra == LRU) m_ra = new LRU_ReplaceAlgorithm(); else if (ra == ARU) m_ra = new ARU_ReplaceAlgorithm(); else if (ra == MRU) m_ra = new MRU_ReplaceAlgorithm(); else m_ra = NULL; } ~Cache() { delete m_ra; } void Replace() { m_ra->Replace(); } }; int main() { Cache cache(LRU); //指定标签即可 cache.Replace(); return 0; }
*/
//Cache需要用到替换算法
template <class RA> class Cache { private: RA m_ra; public: Cache() { } ~Cache() { } void Replace() { m_ra.Replace(); } }; int main() { Cache<LFU_ReplaceAlgorithm> cache; //模板实参 cache.Replace(); return 0; }
相关文章推荐
- poj-2478 Farey Sequence(dp,欧拉函数)
- 简单的四则运算(续)
- discuz登陆相关
- Deep learning:三十二(基础知识_3)
- Deep learning:三十一(数据预处理练习)
- LeetCode 104 Maximum Depth of Binary Tree
- 如何在win7下远程控制ubuntu
- 7.多态
- Qt发布错误:无法定位程序输入点于动态链接库QtCore5.dll
- 类String的构造函数、析构函数和赋值函数
- Maven快速入门
- Android的Binder的起源-android学习之旅(100)
- Android的Binder的起源-android学习之旅(100)
- Android的Binder的起源-android学习之旅(100)
- Deep learning:三十(关于数据预处理的相关技巧)
- Deep learning:二十九(Sparse coding练习)
- C99新特性:变长数组(VLA)
- Linux内核及分析 第三周 Linux内核的启动过程
- 6.面向对象的三大特征
- Arch Linux 安装过程