参考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()
#include <iostream>
#include <string>
using namespace std;
class Person; class Command {
// 1. Create a class that encapsulates an object and a member function
// a pointer to a member function (the attribute's name is "method")
Person *object; //
void(Person::*method)();
public:
Command(Person *obj = , void(Person:: *meth)() = ) {
object = obj; // the argument's name is "meth"
method = meth;
}
void execute(){
(object->*method)(); // invoke the method on the object
}
}; class Person{
string name; // cmd is a "black box", it is a method invocation
// promoted to "full object status"
Command cmd;
public:
Person(string n, Command c) : cmd(c) {
name = n;
}
void talk() {
// "this" is the sender, cmd has the receiver
cout << name << " is talking" << endl;
cmd.execute(); // ask the "black box" to callback the receiver
}
void passOn() {
cout << name << " is passing on" << endl; // 4. When the sender is ready to callback to the receiver,
// it calls execute()
cmd.execute();
}
void gossip() {
cout << name << " is gossiping" << endl;
cmd.execute();
}
void listen() {
cout << name << " is listening" << endl;
}
}; int main(){
// Fred will "execute" Barney which will result in a call to passOn()
// Barney will "execute" Betty which will result in a call to gossip()
// Betty will "execute" Wilma which will result in a call to listen()
Person wilma("Wilma", Command());
// 2. Instantiate an object for each "callback"
// 3. Pass each object to its future "sender"
Person betty("Betty", Command(&wilma, &Person::listen));
Person barney("Barney", Command(&betty, &Person::gossip));
Person fred("Fred", Command(&barney, &Person::passOn));
fred.talk();
}

命令模式 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. PAT_A1135#Is It A Red-Black Tree

    Source: PAT A1135 Is It A Red-Black Tree (30 分) Description: There is a kind of balanced binary sear ...

  2. 图的BFS

    目录: 一.算法的基本思路 二.算法过程 三.题目:785判断是否为二分图 https://blog.csdn.net/weixin_40953222/article/details/80544928 ...

  3. 6.2 C# 2:利用 yield 语句简化迭代器

    class Program { static void Main(string[] args) { object[] values = new object[] { "a", &q ...

  4. 基于分布式框架 Jepsen 的 X-Cluster 正确性测试

    转自:https://mp.weixin.qq.com/s/iOe1VjG1CrHalr_I1PKdKw 原创 2017-08-27 严祥光(祥光) 阿里巴巴数据库技术 1 概述 AliSQL X-C ...

  5. safari浏览器click事件要点击两次才有响应出现闪烁

    闪烁问题 由于在iOS Safari上click事件存在300ms响应延时,所以为touch事件添加样式,会和click事件默认样式叠加而产生闪烁问题. 因为ios safari浏览器中对触摸事件的响 ...

  6. CodeForces 396C On Changing Tree

    On Changing Tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  7. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  8. (6)Spring Boot datasource - mysql【从零开始学Spring Boot】

    在任何一个平台都逃离不了数据库的操作,那么在spring boot中怎么接入数据库呢? 很简单,我们需要在application.properties进行配置一下,application.proper ...

  9. Redis容量及利用计划

    在利用Redis过程当中,咱们发明了很多Redis分歧于Memcached,也差别于MySQL的特点.(本文首要会商Redis未启用VM撑持环境) 1. Schema MySQL: 需事先设计Memc ...

  10. Extensions for Spatial Data

    http://dev.mysql.com/worklog/task/?spm=5176.100239.blogcont4270.8.j3asa7&id=6609 前文: 这两天因为项目原因看了 ...