参考《设计模式:可复用面向对象软件的基础》、《大话设计模式》以及百度
策略模式属于对象行为模式中的一种。
意图: 定义一系列的算法,把它们一个个封装起来,并且使他们可相互替换。策略模式使得算法可独立于使用它的客户而变化。
以缓冲算法为例,缓冲算法有:
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;
}