设计模式笔记--行为型模式之二--Command

 

意图:

将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;

对请求排队或记录请求日志,以及可支持的撤销操作

 

适用性:

1抽象出待执行的动作以参数化某对象,可用过程语言中的回调函数表达这种参数化机制。Command模式是回调机制的一个面向对象的替代品

2在不同的时刻指定,排列和执行请求

3支持取消操作

4支持修改日志

5用构建在原语操作上的高层操作构造一个系统

 

效果:

1将调用操作的对象与知道如何实现该操作的对象解耦合

2可将多个命令装配成一个复合命令

3很容易增加新的Command

 

 

Command.h 

  1. #include <iostream>
  2. using namespace std;
  3. class Command
  4. {
  5. public:
  6.     virtual ~Command() {cout<<"Command is destroyed"<<endl;}
  7.     virtual void Execute() = 0;
  8. };
  9. class Document
  10. {
  11. public:
  12.     Document(const char* s){cout<<"A document is created"<<endl;}
  13.     void Open(){cout<<"Open the document"<<endl;}
  14.     void Paste(){cout<<"Paste the document"<<endl;}
  15.     //Action* _action;
  16. private:
  17.     char* _doc;
  18.     
  19. };
  20. class Application
  21. {
  22. public:
  23.     Application(){cout<<"An application is created"<<endl;}
  24.     Add(Document* ){cout<<"Add an Doucment to the Application"<<endl;}
  25. };
  26. class OpenCommand:public Command
  27. {
  28. public:
  29.     OpenCommand(Application*);
  30.     virtual void Execute();
  31. protected:
  32.     virtual const char* AskUser();
  33. private:
  34.     Application* _application;
  35.     char* _response;
  36. };
  37. class PasteCommand:public Command
  38. {
  39. public:
  40.     PasteCommand(Document* doc){_document = doc;}
  41.     virtual void Execute() {_document->Paste();}
  42. private:
  43.     Document* _document;
  44. };
  45. template <class Receiver>
  46. class SimpleCommand:public Command
  47. {
  48. public:
  49.     
  50.     typedef void (Receiver::*Action)();
  51.     SimpleCommand(Receiver* r,Action a):_receiver(r), _action(a)
  52.     {
  53.     }
  54.     SimpleCommand(Receiver* r,const char* s):_receiver(r), _action(s)
  55.     {
  56.     }
  57.     SimpleCommand(Receiver* r):_receiver(r)
  58.     {
  59.     }
  60.     virtual void Execute();
  61. private:
  62.     Action _action;
  63.     Receiver* _receiver;
  64. };
  65. template <class Receiver> void SimpleCommand<Receiver>::Execute()
  66. {
  67.     _receiver->Paste();
  68. }

 

Command.cpp

 

  1. #include <iostream>
  2. using namespace std;
  3. #include "Command.h"
  4. OpenCommand::OpenCommand(Application* a)
  5. {
  6.     _application = a;
  7. }
  8. void OpenCommand::Execute()
  9. {
  10.     const char* name = AskUser();
  11.     if (name != 0)
  12.     {
  13.         Document* document = new Document(name);
  14.         _application->Add(document);
  15.         document->Open();
  16.     }
  17. }
  18. const char* OpenCommand::AskUser()
  19. {
  20.     char* s="1234";
  21.     return s;
  22. }

 

main.cpp

 

  1. #include "Command.h" 
  2. #include <iostream> 
  3. using namespace std;
  4. int main()
  5. {
  6.     Document* receiver = new Document("1234");
  7.     Command* aCommand = new SimpleCommand <Document>(receiver,&Document::Paste);
  8.     aCommand->Execute();
  9.     return 0;
  10. }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值