目前位置: VCer资源中心 >>> VCer文章 >>> C++/MFC基础

[本帖已阅读1246次 分值80 回复0次] 张贴资源 发回信箱 控制面板

设计模式之 Command 模式

提供者:oases2008 张贴时间:2005-01-01 00:00:00.0 出处:http://www.jblook.cn 作者:不祥

设计模式之 Command 模式(2005-01-01 00:00:00.0)


oases2008


 
级别: VCer小兵
头衔: VCer会员

经验: 258
作品: 3
分会: 华北分会
注册: 2007-01-25 12:00:19.0
登录: 2007-10-30 16:38:36.0
/********************************************************************************

* 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;

class CCommand

{

public:

virtual ~CCommand();

virtual void Execute() = 0;

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();

virtual void 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;

};

#endif

/********************************************************************************

* 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()

{

}

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();

}

}

void CMacroCommand::Add(CCommand*pCmd)

{

_comds.push_back(pCmd);

}

void CMacroCommand::Remove(CCommand *pCmd)

{

_comds.remove(pCmd);

}

本文转载自IT网it求职笔试真题库网

注:转载文章需注明来源:VCer.net 文章地址:http://vcer.net/1000000000370.html

  如果你觉得VCer.net不错,而且你愿意为VCer.net捐赠一元钱,那么点击后面的捐赠按钮吧:) vcer.net捐赠

[回复该贴] [加入个人书签]
[投票结果]

A: 评分 10 0% (0 票)
B: 评分 5 0% (0 票)
C: 评分 0 0% (0 票)
D: 评分 -5 0% (0 票)
E: 评分 -10 0% (0 票)