命令模式 Command design pattern in C++

参考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 = 0, void(Person:: *meth)() = 0) {
        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();
}

原文地址:https://www.cnblogs.com/redips-l/p/10416655.html

时间: 2024-08-30 17:24:14

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

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

命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.net/caroline_wendy/article/details/31379977 命令模式能够用于运行撤销(undo)操作. 详细方法: 1. 对象类中须要保存状态, 如level. package command; public class CeilingFan { String lo

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

命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对象, 以便使用不同的请求\队列\日志来參数化其它对象. 命令模式也能够支持撤销操作. 简单的命令模式的实现: 1. 详细的类, 每个类都有特定的方法: /** * @time 2014年6月9日 */ package command; /** * @author C.L.Wang * */ publ

设计模式 - 命令模式(command pattern) 详解

命令模式(command pattern) 详解 本文地址: http://blog.csdn.net/caroline_wendy 命令模式: 将请求封装成对象, 以便使用不同的请求\队列\日志来参数化其他对象. 命令模式也支持可撤销操作. 命令模式: 调用者(Invoker); 命令(Command): 可执行方法(execute), 具体命令(Concrete Command); 接受者(Receiver): 调用命令(Set Command); 具体方法: 1. 具体对象. /** *

设计模式 - 命令模式(command pattern) 多命令 详解

命令模式(command pattern) 多命令 详解 本文地址: http://blog.csdn.net/caroline_wendy 参考命令模式: http://blog.csdn.net/caroline_wendy/article/details/31379977 具体步骤: 1. 多命令, 把未使用的命令, 初始化为空对象(NoCommand), 根据参数(slot), 选择输出命令. /** * @time 2014年6月16日 */ package command; /**

设计模式 - 命令模式(command pattern) 撤销(undo) 详解

命令模式(command pattern) 撤销(undo) 详解 本文地址: http://blog.csdn.net/caroline_wendy 参考命令模式: http://blog.csdn.net/caroline_wendy/article/details/31379977 命令模式可以用于执行撤销(undo)操作. 具体方法: 1. 对象类中需要保存状态, 如level. package command; public class CeilingFan { String loca

设计模式 - 命令模式(command pattern) 宏命令(macro command) 详解

命令模式(command pattern) 宏命令(macro command) 详解 本文地址: http://blog.csdn.net/caroline_wendy 参考: 命名模式(撤销): http://blog.csdn.net/caroline_wendy/article/details/31419101 命令模式可以执行宏命令(macro command), 即多个命令的组合操作. 具体方法:  1. 其余代码与命令(撤销)一致 2. 添加宏命令(macro command),

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

命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.net/caroline_wendy/article/details/31379977 具体步骤: 1. 多命令, 把未使用的命令, 初始化为空对象(NoCommand), 依据參数(slot), 选择输出命令. /** * @time 2014年6月16日 */ package command; /*

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

设计模式 ( 十三 ) 命令模式Command(对象行为型) 1.概述 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使得请求发送者与请求接收者消除彼此之间的耦合,让对象之间的调用关系更加灵活. 例子1:电视机遥控器 : 遥控器是请求的发送者,电视机是请求的接收者,遥控器上有一些按钮如开,关,换频道等按钮就是具体命令,不同的按钮对应电视机的不同操作. 2.问题

设计模式(行为型)之命令模式(Command Pattern)

PS一句:最终还是选择CSDN来整理发表这几年的知识点,该文章平行迁移到CSDN.因为CSDN也支持MarkDown语法了,牛逼啊! [工匠若水 http://blog.csdn.net/yanbober] 阅读前一篇<设计模式(行为型)之策略模式(Strategy Pattern)>http://blog.csdn.net/yanbober/article/details/45498567 概述 在软件开发中,我们经常需要向某些对象发送请求(调用其中的某个或某些方法),但是并不知道请求的接收