关闭

Strategy策略模式

509人阅读 评论(0) 收藏 举报
分类:
#include <iostream>
using namespace std;


class IStrategy
{
public:
	IStrategy(void){}
	virtual ~IStrategy(void){}
	virtual void Operate(void) = 0;
};

class CContext
{
public:
	CContext(IStrategy *pStrategy)
	{
		this->m_pStrategy = pStrategy;
	}
	~CContext(void)
	{
		delete this->m_pStrategy;
	}

	void Operate(void)
	{
		this->m_pStrategy->Operate();
	}

private:
	IStrategy *m_pStrategy;
};

class CBackDoor : public IStrategy
{
public:

	void Operate(void)
	{
		cout << "调用第一个锦囊" << endl;
	}
};

class CGivenGreenLight :public IStrategy
{
public:
	void Operate(void)
	{
		cout << "调用第二个锦囊" << endl;
	}
};

class CBlockEnemy :public IStrategy
{
public:
	void Operate(void)
	{
		cout << "调用第三个锦囊" << endl;
	}
};
int main()
{
	CContext *pContext;
	
	pContext = new CContext(new CBackDoor());
	pContext->Operate();
	delete pContext;

	pContext = new CContext(new CGivenGreenLight());
	pContext->Operate();
	delete pContext;

	pContext = new CContext(new CBlockEnemy());
	pContext->Operate();
	delete pContext;


	IStrategy* ist  = new CBackDoor();//为什么不直接用这种?
	ist->Operate();
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:563799次
    • 积分:8060
    • 等级:
    • 排名:第2652名
    • 原创:215篇
    • 转载:107篇
    • 译文:11篇
    • 评论:56条
    最新评论