码迷,mamicode.com
首页 > 其他好文 > 详细

设计模型之二策略模式

时间:2015-08-25 23:56:16      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

技术分享


//strategy model
//策略模式是一种定义一系列算法的方法
#include<iostream>

using namespace std;

//一个抽象的策略类
class Strategy{
public:
	virtual void algorithmInterface()=0;
};

//三种不同策略
class StrategyA:public Strategy{
	void algorithmInterface()
	{
		cout<<"A strategy"<<endl;
	}
};

class StrategyB:public Strategy{
	void algorithmInterface()
	{
		cout<<"B strategy"<<endl;
	}
};

class StrategyC:public Strategy{
	void algorithmInterface()
	{
		cout<<"C strategy"<<endl;
	}
};

//维护一个具体策略,具体策略由用户指定
class StrategyContext{
public:
	StrategyContext(char X)
	{//根据用户需要创建策略类
		switch(X)
		{//这里其实就是一个简单工厂模式,实例化一个具体对象
			case 'A':
				stra=new StrategyA();
				break;
			case 'B':
				stra=new StrategyB();
				break;
			case 'C':
				stra=new StrategyC();
				break;
			default:
				break;
		}
	}
	
	void getResult()
	{//根据选择的策略得出结果
		stra->algorithmInterface();
	}

private:
	Strategy* stra;
	//基类指针,用于指向一个具体的子对象
	//这里曾出现过一个编译问题:如果子类是私有继承或保护继承自基类,则基类指针不能指向子类对象,具体原因不明
};

int main()
{
	//客户端只需要知道一个StrategyContext类就可以,而不需要知道其他策略类
	StrategyContext straContxt('B');
	straContxt.getResult();
	return 0;
}


设计模型之二策略模式

标签:

原文地址:http://blog.csdn.net/walker19900515/article/details/47984575

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!