参考https://sourcemaking.com/design_patterns/command/cpp/2

  1. Create a class that encapsulates some number of the following:

    • a "receiver" object
    • the method to invoke
    • the arguments to pass
  2. Instantiate an object for each "callback"
  3. Pass each object to its future "sender"
  4. When the sender is ready to callback to the receiver, it calls execute()
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Person;
  5.  
  6. class Command {
  7. // 1. Create a class that encapsulates an object and a member function
  8. // a pointer to a member function (the attribute's name is "method")
  9. Person *object; //
  10. void(Person::*method)();
  11. public:
  12. Command(Person *obj = , void(Person:: *meth)() = ) {
  13. object = obj; // the argument's name is "meth"
  14. method = meth;
  15. }
  16. void execute(){
  17. (object->*method)(); // invoke the method on the object
  18. }
  19. };
  20.  
  21. class Person{
  22. string name;
  23.  
  24. // cmd is a "black box", it is a method invocation
  25. // promoted to "full object status"
  26. Command cmd;
  27. public:
  28. Person(string n, Command c) : cmd(c) {
  29. name = n;
  30. }
  31. void talk() {
  32. // "this" is the sender, cmd has the receiver
  33. cout << name << " is talking" << endl;
  34. cmd.execute(); // ask the "black box" to callback the receiver
  35. }
  36. void passOn() {
  37. cout << name << " is passing on" << endl;
  38.  
  39. // 4. When the sender is ready to callback to the receiver,
  40. // it calls execute()
  41. cmd.execute();
  42. }
  43. void gossip() {
  44. cout << name << " is gossiping" << endl;
  45. cmd.execute();
  46. }
  47. void listen() {
  48. cout << name << " is listening" << endl;
  49. }
  50. };
  51.  
  52. int main(){
  53. // Fred will "execute" Barney which will result in a call to passOn()
  54. // Barney will "execute" Betty which will result in a call to gossip()
  55. // Betty will "execute" Wilma which will result in a call to listen()
  56. Person wilma("Wilma", Command());
  57. // 2. Instantiate an object for each "callback"
  58. // 3. Pass each object to its future "sender"
  59. Person betty("Betty", Command(&wilma, &Person::listen));
  60. Person barney("Barney", Command(&betty, &Person::gossip));
  61. Person fred("Fred", Command(&barney, &Person::passOn));
  62. fred.talk();
  63. }

命令模式 Command design pattern in C++的更多相关文章

  1. 设计模式 - 命令模式(command pattern) 多命令 具体解释

    命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...

  2. 设计模式 - 命令模式(command pattern) 具体解释

    命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...

  3. 设计模式 - 命令模式(command pattern) 宏命令(macro command) 具体解释

    命令模式(command pattern) 宏命令(macro command) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考: 命名模式(撤销) ...

  4. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  5. 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释

    命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...

  6. 二十四种设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...

  7. 设计模式-15命令模式(Command Pattern)

    1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...

  8. 设计模式 ( 十三 ) 命令模式Command(对象行为型)

    设计模式 ( 十三 ) 命令模式Command(对象行为型) 1.概述         在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需 ...

  9. 命令模式 Command 行为型 设计模式(十八)

    命令模式(Command) 请分析上图中这条命令的涉及到的角色以及执行过程,一种可能的理解方式是这样子的: 涉及角色为:大狗子和大狗子他妈 过程为:大狗子他妈角色 调用 大狗子的“回家吃饭”方法 引子 ...

随机推荐

  1. 【maven】成功生成jar包,提示找不到主类?

    问题描述:   使用maven构建zookeeper项目,完成一个简单的创建组的实例,代码调试完成,使用mvn clean install成功打包得到了jar包,但是在执行时发现使用java -cp ...

  2. 01-Linux命令基础-第01天(命令基础,软件安装与卸载、磁盘管理)

    01-   Linux初步 最早一直是单道程序设计模型的操作系统 69年贝尔实验室决定开发多道程序设计模型的操作系统 Multics计划 (失败了) x86 IA(Intel Architecture ...

  3. X5内核浏览器,video兼容

    使用vue-video-player在移动端微信内置浏览器打开,点击视频自动全屏问题. 参考官方 API 是 H5 同层浏览器的原因,可通过设置video属性来处理. <video-player ...

  4. 转载:Java中的Checked Exception——美丽世界中潜藏的恶魔?

    转自 Amber-Garden 的 博客 https://www.cnblogs.com/loveis715/p/4596551.html 在使用Java编写应用的时候,我们常常需要通过第三方类库来帮 ...

  5. Running to the End(Codeforces & AtCoder 百套计划)

    ...Reserved for the future... 仿照xxy dalao的CF&CC百套计划,做了一个Codeforces & AtCoder 百套计划,按这个速度刷下去,每 ...

  6. 开机进入GRUB不要慌,命令行也可启动Linux

    1. 首先利用ls命令,找到Ubuntu安装在哪个磁盘分区: 比如输入ls后我的机器列出的磁盘分区信息如下: (hd0),(hd1),(hd1,gpt3),(hd1,gpt2),(hd1,gpt1) ...

  7. Ubuntu Server下docker实战 02: docker进阶配置

    在上一篇文章里<Ubuntu Server下docker实战 01: 安装docker>,我们已经把docker安装起来了,并运行了一个hello-world 这一篇,我们继续讲进阶配置. ...

  8. Git 基础教程 之 远程库更新到本地

    PS:git remote -v 查看远程仓库        git diff temp 比较master 分支与temp的不同 如果分支没有合并到主分支上,用        git branch - ...

  9. MYSQL更换密码

    MYSQL5.7以下版本的数据库密码使用的是 mysql这个数据库里的user表的password这个字段, 修改密码只需: 1.update MySQL.user set password=pass ...

  10. EF--code first数据迁移命令

    原文推荐!点我点我! 添加Migrations文件夹,并生成类文件Configuration.cs PM> Enable-Migrations -EnableAutomaticMigration ...