// Strategy.h: interface for the CStrategy class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_STRATEGY_H__174A97DA_5D7A_4183_8361_4B6471820E62__INCLUDED_)
#define AFX_STRATEGY_H__174A97DA_5D7A_4183_8361_4B6471820E62__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CStrategy
{
public:
CStrategy();
virtual ~CStrategy();
public:
virtual void AlgrithmInterface(void) = 0;
};
class CConcreteStrategyA : public CStrategy
{
public:
CConcreteStrategyA();
virtual ~CConcreteStrategyA();
public:
void AlgrithmInterface(void);
};
class CConcreteStrategyB : public CStrategy
{
public:
CConcreteStrategyB();
virtual ~CConcreteStrategyB();
public:
void AlgrithmInterface(void);
};
#endif // !defined(AFX_STRATEGY_H__174A97DA_5D7A_4183_8361_4B6471820E62__INCLUDED_)
// Strategy.cpp: implementation of the CStrategy class.
//
//////////////////////////////////////////////////////////////////////
#include "Strategy.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStrategy::CStrategy()
{
}
CStrategy::~CStrategy()
{
}
//////////////////////////////////////////////////////////////////////
CConcreteStrategyA::CConcreteStrategyA()
{
}
CConcreteStrategyA::~CConcreteStrategyA()
{
}
void CConcreteStrategyA::AlgrithmInterface()
{
cout<<"CConcreteStrategyA::AlgrithmInterface"<<endl;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
CConcreteStrategyB::CConcreteStrategyB()
{
}
CConcreteStrategyB::~CConcreteStrategyB()
{
}
void CConcreteStrategyB::AlgrithmInterface()
{
cout<<"CConcreteStrategyB::AlgrithmInterface"<<endl;
}
//////////////////////////////////////////////////////////////////////
// Context.h: interface for the CContext class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CONTEXT_H__E299AB56_F88B_4134_83B5_DF6A5AFCADE4__INCLUDED_)
#define AFX_CONTEXT_H__E299AB56_F88B_4134_83B5_DF6A5AFCADE4__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CStrategy;
class CContext
{
public:
CContext(CStrategy *pStrategy);
virtual ~CContext();
public:
void DoAction(void);
private:
CStrategy *m_pStrategy;
};
#endif // !defined(AFX_CONTEXT_H__E299AB56_F88B_4134_83B5_DF6A5AFCADE4__INCLUDED_)
// Context.cpp: implementation of the CContext class.
//
//////////////////////////////////////////////////////////////////////
#include "Context.h"
#include "Strategy.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CContext::CContext(CStrategy *pStrategy)
{
m_pStrategy = pStrategy;
}
CContext::~CContext()
{
}
void CContext::DoAction()
{
m_pStrategy->AlgrithmInterface();
}
//main.cpp
#include "Strategy.h"
#include "Context.h"
void main()
{
CStrategy *pStrategy = new CConcreteStrategyB();
CContext *pContext = new CContext(pStrategy);
pContext->DoAction();
delete pContext;
delete pStrategy;
}