Command 命令模式

来源:互联网 发布:mac axure 保存为图片 编辑:程序博客网 时间:2024/05/21 09:04

#include <cstdlib>
#include <iostream>

using namespace std;
class Reciver
{
public:
  virtual void Execute() = 0;
};
class ReciverConcrete:public Reciver
{
public:
  virtual void Execute(){cout<<"ReciverConcrete do sth."<<endl;}
};
class Command
{
  Reciver* pRecv;
public:
  Command(Reciver* p):pRecv(p){}
  ~Command(){delete pRecv;}
  void Execute(){pRecv->Execute();}
};
class Invoker
{
  Command* pCom;
public:
  Invoker(Command* p):pCom(p){}
  ~Invoker(){delete pCom;}
  void Invoke(){pCom->Execute();}
};
void Do(Invoker* pInv)
{
  pInv->Invoke();
  delete pInv;
}
int main(int argc, char *argv[])
{
    Do(new Invoker(new Command(new ReciverConcrete)));
    system("PAUSE");
    return EXIT_SUCCESS;
}