#include "stdafx.h"
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <string>
#include <iostream>
#include <boost/format.hpp>
using namespace boost;
class Strategy
{
public:
virtual void AlgorithmStragegy() = 0;
};
class StrategyA : public Strategy
{
public:
virtual void AlgorithmStragegy()
{
std::cout << "AAAAAAA" << std::endl;
}
};
class StrategyB : public Strategy
{
public:
virtual void AlgorithmStragegy()
{
std::cout << "BBBBBBB" << std::endl;
}
};
class Context
{
public:
Context(shared_ptr<Strategy> strategy)
{
_m_Strategy = strategy;
}
void setStrategy(shared_ptr<Strategy> strategy)
{
_m_Strategy = strategy;
}
void DoAction()
{
_m_Strategy->AlgorithmStragegy();
}
protected:
shared_ptr<Strategy> _m_Strategy;
};
void testStrategy()
{
shared_ptr<StrategyA> strategyA = make_shared<StrategyA>();
auto strategyB = make_shared<StrategyB>();
shared_ptr<Context> context = make_shared<Context>(strategyA);
context->DoAction();
context->setStrategy(strategyB);
context->DoAction();
}
策略模式 Strategy
最新推荐文章于 2024-08-20 22:06:15 发布