Command (C++实现)

// Command.cpp : 定义控制台应用程序的入口点。
//

 

/*

该实现为最简单方式。

 

*/

#include "stdafx.h"
#include <iostream>

using namespace std;

class Command
{
public :
 Command()
 {
 }
 virtual~ Command()
 {
 }
 virtual void Execute()=0;
};

class Receiver
{
public:
 Receiver()
 {
 }
 virtual ~Receiver()
 {
 }
  void Action()
  {
   cout<<"Do  execution in Reveiver"<<endl;
  }
};
class ConcreteCommand:public Command
{
public:
 ConcreteCommand(Receiver * r):m_Preceiver(r)
 {
 }
 virtual ~ConcreteCommand()
 {
 }

 virtual void Execute()
 {
  m_Preceiver->Action();
 }

private:
 Receiver *m_Preceiver;

};

class Invoker
{
public:
  Invoker(Command * m):m_command(m)
 {
  }
  virtual ~Invoker()
  {

  }

  void Invoke()
  {
   if(NULL!=m_command)
        m_command->Execute();
  }

private:
 Command * m_command;
};
int _tmain(int argc, _TCHAR* argv[])
{
 Receiver* r=new Receiver();
 Command *cm=new ConcreteCommand(r);
 Invoker *ik=new Invoker(cm);
 ik->Invoke();
 
 delete r;
 delete cm;
 delete ik;


 return 0;
}

 

posted on 2011-05-29 20:38  IT@民工  阅读(173)  评论(0编辑  收藏  举报

导航