// 策略模式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;
};
//
#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;
};