设计模式(15)-策略模式
来源:互联网 发布:bluestacks mac 编辑:程序博客网 时间:2024/06/17 02:33
策略模式
策略模式属于行为模式,其特点是定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法的变化不会影响到使用算法的客户。
优点:1、 简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试;
2、避免程序中使用多重条件转移语句,使系统更灵活,并易于扩展;
3、遵守大部分GRASP原则和常用设计原则,高内聚、低偶合;
缺点:1、因为每个具体策略类都会产生一个新类,所以会增加系统需要维护的类的数量;
2、在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给策略模式的Context对象;
代码
此处以缓存(Cache)的替换算法为例子来说明策略模式的使用。例子中不同的替换算法均为缓存提供一种替换方式,用户可以随意改变使用任何一种替换算法。
C++代码
文件结构:
—include
——ReplaceAlgorithm.h
——LRUReplaceAlgorithm.h
——FIFOReplaceAlgorithm.h
——RANDOMReplaceAlgorithm.h
——Cache.h
—src
——ReplaceAlgorithm.cpp
——LRUReplaceAlgorithm.cpp
——FIFOReplaceAlgorithm.cpp
——RANDOMReplaceAlgorithm.cpp
——Cache.cpp
——main.cpp
代码如下:
—include/ReplaceAlgorithm.h
#ifndef _REPLACEALGORITHM_H_#define _REPLACEALGORITHM_H_class ReplaceAlgorithm{public: virtual ~ReplaceAlgorithm(); virtual void Replace()=0;};#endif
—include/LRUReplaceAlgorithm.h
#ifndef _LRUREPLACEALGORITHM_H_#define _LRUREPLACEALGORITHM_H_#include"ReplaceAlgorithm.h"class LRUReplaceAlgorithm:public ReplaceAlgorithm{public: ~LRUReplaceAlgorithm(); virtual void Replace();};#endif
—include/FIFOReplaceAlgorithm.h
#ifndef _FIFOREPLACEALGORITHM_H_#define _FIFOREPLACEALGORITHM_H_#include"ReplaceAlgorithm.h"class FIFOReplaceAlgorithm:public ReplaceAlgorithm{public: ~FIFOReplaceAlgorithm(); virtual void Replace();};#endif
—include/RANDOMReplaceAlgorithm.h
#ifndef _RANDOMREPLACEALGORITHM_H_#define _RANDOMREPLACEALGORITHM_H_#include"ReplaceAlgorithm.h"class RANDOMReplaceAlgorithm:public ReplaceAlgorithm{public: ~RANDOMReplaceAlgorithm(); virtual void Replace();};#endif
—include/Cache.h
#ifndef _CACHE_H_#define _CACHE_H_#include"ReplaceAlgorithm.h"#include<memory>class Cache{public: Cache(); ~Cache(); void Replace(); void SetReplaceAlgorithm(std::shared_ptr<ReplaceAlgorithm> ptr);private: std::shared_ptr<ReplaceAlgorithm> replacePtr;};#endif
—src/ReplaceAlgorithm.cpp
#include"ReplaceAlgorithm.h"ReplaceAlgorithm::~ReplaceAlgorithm(){ ;}
—src/LRUReplaceAlgorithm.cpp
#include"LRUReplaceAlgorithm.h"#include<iostream>using namespace std;LRUReplaceAlgorithm::~LRUReplaceAlgorithm(){ ;}void LRUReplaceAlgorithm::Replace(){ cout<<"Run LRU Algorithm to replace Cache."<<endl; }
—src/FIFOReplaceAlgorithm.cpp
#include"FIFOReplaceAlgorithm.h"#include<iostream>using namespace std;FIFOReplaceAlgorithm::~FIFOReplaceAlgorithm(){ ;}void FIFOReplaceAlgorithm::Replace(){ cout<<"Run FIFO Algorithm to replace Cache."<<endl;}
—src/RANDOMReplaceAlgorithm.cpp
#include"RANDOMReplaceAlgorithm.h"#include<iostream>using namespace std;RANDOMReplaceAlgorithm::~RANDOMReplaceAlgorithm(){ ;}void RANDOMReplaceAlgorithm::Replace(){ cout<<"Run RANDOM Algorithm to replace Cache."<<endl;}
—src/Cache.cpp
#include"Cache.h"#include<iostream>#include<memory>#include"LRUReplaceAlgorithm.h"#include"FIFOReplaceAlgorithm.h"#include"RANDOMReplaceAlgorithm.h"using namespace std;Cache::Cache(){ replacePtr = nullptr;}Cache::~Cache(){ ;}void Cache::Replace(){ if(nullptr == replacePtr) { cout<<"No Algorithm to replace Cache."<<endl; } else { replacePtr->Replace(); }}void Cache::SetReplaceAlgorithm(shared_ptr<ReplaceAlgorithm> ptr){ replacePtr = ptr;}
—src/main.cpp
#include"Cache.h"#include"LRUReplaceAlgorithm.h"#include"FIFOReplaceAlgorithm.h"#include"RANDOMReplaceAlgorithm.h"#include<iostream>#include<memory>using namespace std;int main(){ Cache cache; cache.Replace(); shared_ptr<ReplaceAlgorithm> repPtr = make_shared<LRUReplaceAlgorithm>(); cache.SetReplaceAlgorithm(repPtr); cache.Replace(); repPtr = make_shared<FIFOReplaceAlgorithm> (); cache.SetReplaceAlgorithm(repPtr); cache.Replace(); repPtr = make_shared<RANDOMReplaceAlgorithm> (); cache.SetReplaceAlgorithm(repPtr); cache.Replace(); return 0;}
Python代码
文件结构:
—ReplaceAlgorithm.py
—Cache.py
—main.py
代码如下:
—ReplaceAlgorithm.py
# -*- coding:utf-8 -*-class ReplaceAlgorithm: def Replace(self): passclass LRUReplaceAlgorithm(ReplaceAlgorithm): def Replace(self): print "Run LRU Algorithm to replace Cache."class FIFOReplaceAlgorithm(ReplaceAlgorithm): def Replace(self): print "Run FIFO Algorithm to replace Cache."class RANDOMReplaceAlgorithm(ReplaceAlgorithm): def Replace(self): print "Run RANDOM Algorithm to replace Cache."
—Cache.py
# -*- coding:utf-8 -*-from ReplaceAlgorithm import *class Cache: def __init__(self): self.__replaceAlg = None def Replace(self): if(self.__replaceAlg == None): print "No Algorithm to replace Cache." else: self.__replaceAlg.Replace() def SetReplaceAlgorithm(self, replaceAlg): self.__replaceAlg = replaceAlg
—main.py
# -*- coding:utf-8 -*-from Cache import *from ReplaceAlgorithm import *if "__main__" == __name__: cache = Cache() cache.Replace() algorithm = LRUReplaceAlgorithm() cache.SetReplaceAlgorithm(algorithm) cache.Replace() algorithm = FIFOReplaceAlgorithm() cache.SetReplaceAlgorithm(algorithm) cache.Replace() algorithm = RANDOMReplaceAlgorithm() cache.SetReplaceAlgorithm(algorithm) cache.Replace()
- 设计模式(15)-策略模式
- 设计模式-策略模式
- 设计模式:策略模式
- 设计模式-策略模式
- 设计模式 - 策略模式
- 设计模式-策略模式
- 设计模式-----策略模式
- 设计模式 策略模式
- 设计模式-策略模式
- 设计模式-【策略模式】
- 设计模式-----策略模式
- 设计模式-策略模式
- 设计模式--策略模式
- 设计模式- 策略模式
- 设计模式- 策略模式
- 设计模式------策略模式
- 设计模式--策略模式
- 设计模式-策略模式
- C++抽象编程——字符串(3)——<cctype>库在字符串中的应用
- Android MVC MVP MVVM 模式
- jvm动态代理原理
- leetcode141. Linked List Cycle
- iOS沙盒目录结构解析
- 设计模式(15)-策略模式
- 线程的消息机制
- stm32低功耗模式总结
- 【Android 性能优化】—— 详解内存优化的来龙去脉
- C#基础-string类
- 【Oracle】虚表的使用
- 加快android studio 编译速度
- 【BZOJ 3230】相似子串 后缀数组
- DBCP1.3连接泄露问题