设计模式之策略模式(Strategy)

参考《设计模式:可复用面向对象软件的基础》、《大话设计模式》以及百度

策略模式属于对象行为模式中的一种。

意图: 定义一系列的算法,把它们一个个封装起来,并且使他们可相互替换。策略模式使得算法可独立于使用它的客户而变化。

以缓冲算法为例,缓冲算法有:

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;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值