策略模式testing1002.cpp

来源:互联网 发布:java怎么学 编辑:程序博客网 时间:2024/05/17 06:48
// 策略模式testing1002.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include<iostream>
using namespace std;


class Strategy
{
public:
virtual void show(){}
};


class StrategyA:public Strategy
{
public:
virtual void show(){cout<<"this is strategy A";}
};
class StrategyB:public Strategy
{
public:
virtual void show(){cout<<"this is strategy B";};
};


class Context
{
private:
Strategy& _strategy;
public:
Context(Strategy&  strategy):_strategy(strategy)
{};
void show(){ _strategy.show(); };
};


int main()
{
Context* context=new Context(StrategyA());
context->show();
delete context;
return 0;
};
原创粉丝点击