Welcome to tiger's blog!

What lead to success, what we are seeking...
posts - 47, comments - 23, trackbacks - 0, articles - 8
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

设计模式(Strategy)

Posted on 2007-04-14 15:04 tiger 阅读(333) 评论(0)  编辑 收藏 引用

// 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;
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理