命令模式 Command design pattern in C++
参考https://sourcemaking.com/design_patterns/command/cpp/2
- Create a class that encapsulates some number of the following:
- a "receiver" object
- the method to invoke
- the arguments to pass
- Instantiate an object for each "callback"
- Pass each object to its future "sender"
- 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++的更多相关文章
- 设计模式 - 命令模式(command pattern) 多命令 具体解释
命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...
- 设计模式 - 命令模式(command pattern) 具体解释
命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...
- 设计模式 - 命令模式(command pattern) 宏命令(macro command) 具体解释
命令模式(command pattern) 宏命令(macro command) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考: 命名模式(撤销) ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
- 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释
命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...
- 二十四种设计模式:命令模式(Command Pattern)
命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...
- 设计模式-15命令模式(Command Pattern)
1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
设计模式 ( 十三 ) 命令模式Command(对象行为型) 1.概述 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需 ...
- 命令模式 Command 行为型 设计模式(十八)
命令模式(Command) 请分析上图中这条命令的涉及到的角色以及执行过程,一种可能的理解方式是这样子的: 涉及角色为:大狗子和大狗子他妈 过程为:大狗子他妈角色 调用 大狗子的“回家吃饭”方法 引子 ...
随机推荐
- [USACO5.5]隐藏口令Hidden Password [最小表示法模板]
最小表示法就是一个字符串构成一个环,找以哪个点为开头字典序最小. 然后我们就可以用n2的算法愉快的做啦~实际上有O(n)的做法的,就是用两个指针扫,如果这两个位置的字典序相等,就一起往后,如果某一个大 ...
- codevs——T1220 数字三角形
http://codevs.cn/problem/1043/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- LNMP一键安装包 V1.1 公布
LNMP一键安装包 是一个用Linux Shell编写的能够为CentOS/RadHat.Debian/Ubuntu VPS(VDS)或独立主机安装LNMP(Nginx.MySQL/MariaDB.P ...
- Light oj 1251 - Forming the Council 【2-sat】【推断是否存在可行解 + 反向拓扑输出可行解】
1251 - Forming the Council problem=1251" style="color:rgb(79,107,114)"> PDF (Engli ...
- mysql稳定的版本号选择及下载说明(2014-11-10)
怎样选择新稳定的版本号 mysql的版本号大概能够分为Alpha.Beta.GA. GA版即mysql官方公布的稳定版本号. 怎样在官方下载Mysql 能够通过http:// ...
- CefSharp 设置cookie
设置cookie var cookieManager = CefSharp.Cef.GetGlobalCookieManager(); await cookieManager.SetCookieAsy ...
- cefsharp 获取高度
G.browser.GetBrowser().MainFrame.ExecuteJavaScriptAsync("$(document).height()"); // Get Do ...
- java根据内容生成二维码图片
package util; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; ...
- graphviz.js划线操作
digraph A{ graph[color=red bgcolor="cadetblue" label="海阔天空",fontname="FangS ...
- 0x63树的直径与最近公共祖先
凉 bzoj1999 先把树的直径求出来,从左往右枚举,对于当前位置i,找到满足限制并且最远的点j,当前位置最大值就是max(i~j区间内除直径外的子树路径长度最大值,1~i的长度,j~n的长度) 然 ...