/********************************************************************************
* File Name : Command.h
*
* EMail Addr : seakingw@163.com
*
* Description : interface for the CCommand class.
*
********************************************************************************/
* File Name : Command.h
*
* EMail Addr : seakingw@163.com
*
* Description : interface for the CCommand class.
*
********************************************************************************/
#ifndef _COMMOND_PATTERNS
#define _COMMOND_PATTERNS
#include <list>
class CCommand;
typedef std::list<CCommand *> CommandList;
typedef CommandList::iterator CommandListIt;
typedef CommandList::reverse_iterator CommandListReIt;
typedef CommandList::iterator CommandListIt;
typedef CommandList::reverse_iterator CommandListReIt;
class CCommand
{
public:
virtual ~CCommand();
{
public:
virtual ~CCommand();
virtual void Execute() = 0;
virtual void UnExecute() = 0;
protected:
CCommand();
};
virtual void UnExecute() = 0;
protected:
CCommand();
};
template <class Receiver>
class CSimpleCommand:public CCommand
{
public:
typedef void (Receiver::*Action) ();
CSimpleCommand(Receiver* r, Action a):
_receiver(r),_action(a)
{
}
virtual ~CSimpleCommand();
{
public:
typedef void (Receiver::*Action) ();
CSimpleCommand(Receiver* r, Action a):
_receiver(r),_action(a)
{
}
virtual ~CSimpleCommand();
virtual void Execute();
private:
Action _action;
Receiver* _receiver;
};
//myCloass receiver = new MyClass;
//...
//CCommand *aCommand = new CSimpleCommand<MyClass> (receiver ,&MyClass::Action);
//...
//aCommand->Execute();
private:
Action _action;
Receiver* _receiver;
};
//myCloass receiver = new MyClass;
//...
//CCommand *aCommand = new CSimpleCommand<MyClass> (receiver ,&MyClass::Action);
//...
//aCommand->Execute();
class CMacroCommand :public CCommand
{
public:
CMacroCommand();
virtual ~CMacroCommand();
virtual void Add(CCommand*);
virtual void Remove(CCommand*);
virtual void Execute();
virtual void UnExecute();
private:
CommandList _comds;
};
virtual void Remove(CCommand*);
virtual void Execute();
virtual void UnExecute();
private:
CommandList _comds;
};
#endif
/********************************************************************************
* File Name : Command.cpp
*
* EMail Addr : seakingw@163.com
*
*
* Description : implementation of the CCommand class.
*
********************************************************************************/
* File Name : Command.cpp
*
* EMail Addr : seakingw@163.com
*
*
* Description : implementation of the CCommand class.
*
********************************************************************************/
#include "Command.h"
CCommand::CCommand()
{
}
{
}
CCommand::~CCommand()
{
}
{
}
template <class Receiver>
void CSimpleCommand<Receiver> ::Execute()
{
(_receiver->*_action)();
}
template <class Receiver>
CSimpleCommand<Receiver>::~CSimpleCommand()
{
}
void CSimpleCommand<Receiver> ::Execute()
{
(_receiver->*_action)();
}
template <class Receiver>
CSimpleCommand<Receiver>::~CSimpleCommand()
{
}
CMacroCommand::CMacroCommand()
{
}
{
}
CMacroCommand::~CMacroCommand()
{
}
{
}
void CMacroCommand::Execute()
{
CommandListIt it ;
for(it = _comds.begin(); it != _comds.end();it++)
{
CCommand *pCmd = *it;
pCmd->Execute();
}
}
void CMacroCommand::UnExecute()
{
CommandListReIt reIt ;
for(reIt = _comds.rbegin(); reIt != _comds.rend();reIt++)
{
CCommand *pCmd = *reIt;
pCmd->UnExecute();
}
}
{
CommandListIt it ;
for(it = _comds.begin(); it != _comds.end();it++)
{
CCommand *pCmd = *it;
pCmd->Execute();
}
}
void CMacroCommand::UnExecute()
{
CommandListReIt reIt ;
for(reIt = _comds.rbegin(); reIt != _comds.rend();reIt++)
{
CCommand *pCmd = *reIt;
pCmd->UnExecute();
}
}
void CMacroCommand::Add(CCommand*pCmd)
{
_comds.push_back(pCmd);
}
{
_comds.push_back(pCmd);
}
void CMacroCommand::Remove(CCommand *pCmd)
{
_comds.remove(pCmd);
}
{
_comds.remove(pCmd);
}