命令模式 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) 请分析上图中这条命令的涉及到的角色以及执行过程,一种可能的理解方式是这样子的: 涉及角色为:大狗子和大狗子他妈 过程为:大狗子他妈角色 调用 大狗子的“回家吃饭”方法 引子 ...
随机推荐
- 【maven】成功生成jar包,提示找不到主类?
问题描述: 使用maven构建zookeeper项目,完成一个简单的创建组的实例,代码调试完成,使用mvn clean install成功打包得到了jar包,但是在执行时发现使用java -cp ...
- 01-Linux命令基础-第01天(命令基础,软件安装与卸载、磁盘管理)
01- Linux初步 最早一直是单道程序设计模型的操作系统 69年贝尔实验室决定开发多道程序设计模型的操作系统 Multics计划 (失败了) x86 IA(Intel Architecture ...
- X5内核浏览器,video兼容
使用vue-video-player在移动端微信内置浏览器打开,点击视频自动全屏问题. 参考官方 API 是 H5 同层浏览器的原因,可通过设置video属性来处理. <video-player ...
- 转载:Java中的Checked Exception——美丽世界中潜藏的恶魔?
转自 Amber-Garden 的 博客 https://www.cnblogs.com/loveis715/p/4596551.html 在使用Java编写应用的时候,我们常常需要通过第三方类库来帮 ...
- Running to the End(Codeforces & AtCoder 百套计划)
...Reserved for the future... 仿照xxy dalao的CF&CC百套计划,做了一个Codeforces & AtCoder 百套计划,按这个速度刷下去,每 ...
- 开机进入GRUB不要慌,命令行也可启动Linux
1. 首先利用ls命令,找到Ubuntu安装在哪个磁盘分区: 比如输入ls后我的机器列出的磁盘分区信息如下: (hd0),(hd1),(hd1,gpt3),(hd1,gpt2),(hd1,gpt1) ...
- Ubuntu Server下docker实战 02: docker进阶配置
在上一篇文章里<Ubuntu Server下docker实战 01: 安装docker>,我们已经把docker安装起来了,并运行了一个hello-world 这一篇,我们继续讲进阶配置. ...
- Git 基础教程 之 远程库更新到本地
PS:git remote -v 查看远程仓库 git diff temp 比较master 分支与temp的不同 如果分支没有合并到主分支上,用 git branch - ...
- MYSQL更换密码
MYSQL5.7以下版本的数据库密码使用的是 mysql这个数据库里的user表的password这个字段, 修改密码只需: 1.update MySQL.user set password=pass ...
- EF--code first数据迁移命令
原文推荐!点我点我! 添加Migrations文件夹,并生成类文件Configuration.cs PM> Enable-Migrations -EnableAutomaticMigration ...