jack-wang
小王
C++博客
首页
新随笔
联系
聚合
管理
随笔-371 评论-37 文章-0 trackbacks-0
常见设计模式的解析和实现(C++)之十四-Command模式
转:
http://www.cppblog.com/converse/archive/2006/08/04/10855.html
作用:
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作.
UML结构图:
解析:
Comnand模式的思想是把命令封装在一个类中,就是这里的Command基类,同时把接收对象也封装在一个类中就是这里的Receiver类中,由调用这个命令的类也就是这里的Invoker类来调用.其实,如果弄清楚了Command模式的原理,就会发现其实它和注册回调函数的原理是很相似的,而在面向过程的设计中的回调函数其实和这里的Command类的作用是一致的.采用Command模式解耦了命令的发出者和命令的执行者.
实现:
1)Command.h
/**/
/*
*******************************************************************
created: 2006/08/04
filename: Command.h
author: 李创
http://www.cppblog.com/converse/
purpose: Command模式的演示代码
********************************************************************
*/
#ifndef COMMAND_H
#define
COMMAND_H
class
Command
{
public
:
virtual
~
Command()
{}
virtual
void
Execute()
=
0
;
}
;
class
Receiver
{
public
:
void
Action();
}
;
class
Invoker
{
public
:
Invoker(Command
*
pCommand);
~
Invoker();
void
Invoke();
private
:
Command
*
m_pCommand;
}
;
class
ConcreateComand
:
public
Command
{
public
:
ConcreateComand(Receiver
*
pReceiver);
virtual
~
ConcreateComand();
virtual
void
Execute();
private
:
Receiver
*
m_pReceiver;
}
;
#endif
2)Command.cpp
/**/
/*
*******************************************************************
created: 2006/08/04
filename: Command.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: Command模式的演示代码
********************************************************************
*/
#include
"
Command.h
"
#include
<
iostream
>
void
Receiver::Action()
{
std::cout
<<
"
Receiver Action\n
"
;
}
Invoker::Invoker(Command
*
pCommand)
: m_pCommand(pCommand)
{
}
Invoker::
~
Invoker()
{
delete m_pCommand;
m_pCommand
=
NULL;
}
void
Invoker::Invoke()
{
if
(NULL
!=
m_pCommand)
{
m_pCommand
->
Execute();
}
}
ConcreateComand::ConcreateComand(Receiver
*
pReceiver)
: m_pReceiver(pReceiver)
{
}
ConcreateComand::
~
ConcreateComand()
{
delete m_pReceiver;
m_pReceiver
=
NULL;
}
void
ConcreateComand::Execute()
{
if
(NULL
!=
m_pReceiver)
{
m_pReceiver
->
Action();
}
std::cout
<<
"
Execute by ConcreateComand\n
"
;
}
3)Main.cpp
/**/
/*
*******************************************************************
created: 2006/08/04
filename: main.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: Command模式的测试代码
********************************************************************
*/
#include
"
Command.h
"
#include
<
stdlib.h
>
int
main()
{
Receiver
*
pReceiver
=
new
Receiver();
Command
*
pCommand
=
new
ConcreateComand(pReceiver);
Invoker
*
pInvoker
=
new
Invoker(pCommand);
pInvoker
->
Invoke();
delete pInvoker;
system(
"
pause
"
);
return
0
;
}
posted on 2011-08-05 01:50
小王
阅读(300)
评论(0)
编辑
收藏
引用
所属分类:
设计模式
只有注册用户
登录
后才能发表评论。
相关文章:
常见设计模式的解析和实现(C++)之十四-Command模式
常见设计模式的解析和实现(C++)之十二-ChainOfResponsibility模式
Command模式
Adapter模式
Facade模式
抽象工厂(Abstract Factory)
工厂方法(factory-method)模式
网站导航:
博客园
IT新闻
BlogJava
知识库
博问
管理
<
2015年6月
>
日
一
二
三
四
五
六
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(16)
给我留言
查看公开留言
查看私人留言
随笔分类
(433)
Android(7)
Boost(8)
C#
c++ 程序设计基础(11)
CMake(2)
Cocos2d-X(1)
CUDA(3)
DB(21)
DirectX(2)
Docker(5)
Dubbo(3)
Erlang(5)
Git(6)
GO(1)
IE(1)
iOS(1)
Java(19)
JPA(2)
LibTorch(1)
linux(97)
MQTT(2)
node.js(3)
OpenGL(2)
Python(12)
Qt(7)
Redis(5)
ROS(4)
SpringBoot(4)
TensorRT(3)
UI(5)
Unreal Engine(1)
VC(44)
VLC(2)
Web开发(12)
Win32(4)
编译(32)
操作系统(3)
调试(2)
多核编程(3)
分布式系统(4)
汇编(1)
脚本(1)
开源项目(3)
其他(16)
嵌入式(1)
软件工程(5)
设计模式(7)
算法与数据结构(1)
网络通讯(17)
音视频(7)
游戏服务器端开发(17)
游戏引擎(7)
随笔档案
(371)
2024年5月 (1)
2024年4月 (4)
2024年3月 (9)
2024年2月 (1)
2024年1月 (6)
2023年12月 (2)
2023年10月 (8)
2023年9月 (1)
2023年7月 (2)
2023年5月 (1)
2023年4月 (3)
2023年3月 (2)
2022年12月 (2)
2022年11月 (3)
2022年10月 (3)
2022年9月 (5)
2022年8月 (2)
2022年7月 (10)
2022年6月 (5)
2022年5月 (7)
2022年4月 (4)
2022年3月 (1)
2022年2月 (11)
2022年1月 (6)
2021年12月 (7)
2021年10月 (3)
2021年6月 (2)
2021年4月 (1)
2021年3月 (3)
2021年2月 (1)
2021年1月 (3)
2020年12月 (2)
2020年11月 (1)
2020年10月 (2)
2020年9月 (2)
2020年7月 (4)
2020年6月 (2)
2020年4月 (3)
2020年3月 (2)
2020年2月 (2)
2020年1月 (3)
2019年11月 (2)
2019年10月 (5)
2019年9月 (1)
2019年8月 (3)
2019年7月 (1)
2019年6月 (6)
2019年5月 (4)
2019年4月 (2)
2019年3月 (2)
2019年2月 (1)
2019年1月 (4)
2018年1月 (2)
2017年12月 (8)
2017年11月 (3)
2017年9月 (4)
2017年8月 (1)
2017年3月 (1)
2017年2月 (2)
2017年1月 (5)
2016年12月 (1)
2016年5月 (1)
2016年4月 (1)
2016年1月 (1)
2015年8月 (3)
2015年6月 (1)
2015年5月 (1)
2015年4月 (1)
2014年7月 (2)
2014年6月 (2)
2014年5月 (1)
2014年3月 (1)
2014年2月 (2)
2013年11月 (3)
2013年9月 (4)
2013年8月 (1)
2013年6月 (1)
2013年5月 (1)
2013年4月 (3)
2013年3月 (5)
2013年2月 (1)
2013年1月 (2)
2012年11月 (1)
2012年10月 (3)
2012年9月 (1)
2012年7月 (3)
2012年6月 (3)
2012年5月 (1)
2012年3月 (5)
2012年2月 (2)
2012年1月 (1)
2011年12月 (3)
2011年9月 (1)
2011年8月 (2)
2011年6月 (1)
2011年4月 (1)
2011年3月 (2)
2011年2月 (2)
2010年12月 (1)
2010年11月 (7)
2010年10月 (7)
2010年9月 (2)
2010年8月 (2)
2010年7月 (3)
2010年6月 (2)
2010年4月 (4)
2010年3月 (6)
2010年2月 (12)
2010年1月 (22)
2009年11月 (6)
2009年8月 (5)
2009年6月 (2)
2009年2月 (4)
2009年1月 (15)
2008年10月 (1)
Linux
chinaunix
游戏开发
金庆
云风
综合
Intel
λ-calculus
周伟明
最新随笔
1. git clone报错:SSL certificate problem: self signed certificate in certificate chain
2. openEuler系统中修改系统时间
3. 在CANN推理程序中,执行aclmdlExecute()函数失败。 返回错误码:507011
4. 导入ffmpeg头文件,编译报错:undefined reference to `avformat_open_input,,,
5. 编译安装ffmpeg后,代码中include ffmpeg头文件,报错找不到此头文件
6. 编译安装opencv4之后,代码中#include
报错:No such file or directory
7. openEuler重启网络命令
8. 新安装的Ubuntu22中无法apt安装软件,报错:Failed to fetch http://cn.archive.ubuntu.com/,,,,,,
9. x86服务器下执行arm64程序报错:/lib64/ld-linux-aarch64.so.1: No such file or directory
10. 编译配置ffmpeg报错:ERROR: x264 not found using pkg-config
搜索
最新随笔
1. git clone报错:SSL certificate problem: self signed certificate in certificate chain
2. openEuler系统中修改系统时间
3. 在CANN推理程序中,执行aclmdlExecute()函数失败。 返回错误码:507011
4. 导入ffmpeg头文件,编译报错:undefined reference to `avformat_open_input,,,
5. 编译安装ffmpeg后,代码中include ffmpeg头文件,报错找不到此头文件
6. 编译安装opencv4之后,代码中#include
报错:No such file or directory
7. openEuler重启网络命令
8. 新安装的Ubuntu22中无法apt安装软件,报错:Failed to fetch http://cn.archive.ubuntu.com/,,,,,,
9. x86服务器下执行arm64程序报错:/lib64/ld-linux-aarch64.so.1: No such file or directory
10. 编译配置ffmpeg报错:ERROR: x264 not found using pkg-config
最新评论
1. re: DirectUI Lib XML编写说明
这个不错,很有用。
--dictbox
2. re: MFC:为CListCtrl添加背景图片[未登录]
没用
--123
3. re: DirectUI Lib XML编写说明[未登录]
很好,对于我这样的初学者很用帮助,谢谢楼主
--king
4. re: WindowXP下PHP5开发环境配置
谢谢楼主分享,已经按楼主的方法配置成功
--bbreay
5. re: error C2220: 警告被视为错误 - 没有生成“object”文件
你好,我用的是vs2012,没有你说的“选择该cpp”,如:
--coco
阅读排行榜
1. protobuf使用方法(9335)
2. 1>c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afx.h(24) : fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d](8537)
3. 执行pip install报错: WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv(8380)
4. 编译cmake报错:Cannot find a C++ compiler that supports both C++11 and the specified C++ flags.(7137)
5. 把python3的版本从3.6升级到3.10(6383)
评论排行榜
1. Ubuntu9.10 VI下方向键变成ABCD的解决办法(3)
2. 公司散伙啦。杯具!反思!(3)
3. 网游服务器通信架构的设计(3)
4. 服务器宕机(2)
5. 没有找到MSVCR90.dll,因此这个应用程序未能启动,重新安装应用程序可能会修复此问题(2)
60天内阅读排行
1. openEuler重启网络命令(768)
2. openEuler系统中修改系统时间(150)
3. 编译配置ffmpeg报错:ERROR: x264 not found using pkg-config(44)
4. x86服务器下执行arm64程序报错:/lib64/ld-linux-aarch64.so.1: No such file or directory(40)
5. 编译安装opencv4之后,代码中#include
报错:No such file or directory(34)