jack-wang
小王
C++博客
首頁(yè)
新隨筆
聯(lián)系
聚合
管理
隨筆-379 評(píng)論-37 文章-0 trackbacks-0
常見(jiàn)設(shè)計(jì)模式的解析和實(shí)現(xiàn)(C++)之十四-Command模式
轉(zhuǎn):
http://m.shnenglu.com/converse/archive/2006/08/04/10855.html
作用:
將一個(gè)請(qǐng)求封裝為一個(gè)對(duì)象,從而使你可用不同的請(qǐng)求對(duì)客戶進(jìn)行參數(shù)化;對(duì)請(qǐng)求排隊(duì)或記錄請(qǐng)求日志,以及支持可撤消的操作.
UML結(jié)構(gòu)圖:
解析:
Comnand模式的思想是把命令封裝在一個(gè)類中,就是這里的Command基類,同時(shí)把接收對(duì)象也封裝在一個(gè)類中就是這里的Receiver類中,由調(diào)用這個(gè)命令的類也就是這里的Invoker類來(lái)調(diào)用.其實(shí),如果弄清楚了Command模式的原理,就會(huì)發(fā)現(xiàn)其實(shí)它和注冊(cè)回調(diào)函數(shù)的原理是很相似的,而在面向過(guò)程的設(shè)計(jì)中的回調(diào)函數(shù)其實(shí)和這里的Command類的作用是一致的.采用Command模式解耦了命令的發(fā)出者和命令的執(zhí)行者.
實(shí)現(xiàn):
1)Command.h
/**/
/*
*******************************************************************
created: 2006/08/04
filename: Command.h
author: 李創(chuàng)
http://m.shnenglu.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: 李創(chuàng)
http://m.shnenglu.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: 李創(chuàng)
http://m.shnenglu.com/converse/
purpose: Command模式的測(cè)試代碼
********************************************************************
*/
#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
小王
閱讀(322)
評(píng)論(0)
編輯
收藏
引用
所屬分類:
設(shè)計(jì)模式
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開(kāi)源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
常見(jiàn)設(shè)計(jì)模式的解析和實(shí)現(xiàn)(C++)之十四-Command模式
常見(jiàn)設(shè)計(jì)模式的解析和實(shí)現(xiàn)(C++)之十二-ChainOfResponsibility模式
Command模式
Adapter模式
Facade模式
抽象工廠(Abstract Factory)
工廠方法(factory-method)模式
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問(wèn)
Chat2DB
管理
<
2015年4月
>
日
一
二
三
四
五
六
29
30
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
常用鏈接
我的隨筆
我的評(píng)論
我參與的隨筆
留言簿
(16)
給我留言
查看公開(kāi)留言
查看私人留言
隨筆分類
(441)
Android(7)
Boost(8)
C#
c++ 程序設(shè)計(jì)基礎(chǔ)(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(99)
MQTT(2)
node.js(3)
OpenGL(2)
Python(15)
Qt(7)
Redis(5)
ROS(4)
SpringBoot(4)
TensorRT(3)
UI(5)
Unreal Engine(1)
VC(44)
VLC(2)
Web開(kāi)發(fā)(12)
Win32(4)
編譯(34)
操作系統(tǒng)(3)
調(diào)試(2)
多核編程(3)
分布式系統(tǒng)(4)
匯編(1)
腳本(1)
開(kāi)源項(xiàng)目(3)
其他(16)
嵌入式(1)
軟件工程(5)
瑞芯微(1)
設(shè)計(jì)模式(7)
算法與數(shù)據(jù)結(jié)構(gòu)(1)
網(wǎng)絡(luò)通訊(17)
音視頻(7)
游戲服務(wù)器端開(kāi)發(fā)(17)
游戲引擎(7)
隨筆檔案
(379)
2024年11月 (2)
2024年10月 (1)
2024年6月 (2)
2024年5月 (4)
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
游戲開(kāi)發(fā)
金慶
云風(fēng)
綜合
Intel
λ-calculus
周偉明
最新隨筆
1.?dnf安裝失敗
2.?RK3588設(shè)備中運(yùn)行可執(zhí)行程序報(bào)錯(cuò):error while loading shared libraries: librknnrt.so: cannot open shared object file:
3.?wget下載報(bào)錯(cuò):The certificate of ‘www.python.org’ is not trusted.
4.?執(zhí)行torch.load(模型名稱, map_location='cpu')報(bào)錯(cuò):from torchvision.transforms.functional_tensor import rgb_to_grayscale
5.?pip安裝basicsr報(bào)錯(cuò):To fix this you could try to:
6.?cmake文件中D_GLIBCXX_USE_CXX11_ABI=0,導(dǎo)致無(wú)法到入第三方庫(kù)libjsoncpp.so
7.?鏈接libjsoncpp.a時(shí)報(bào)錯(cuò):which may bind externally can not be used when making a shared object; recompile with -fPIC
8.?vs code中g(shù)it push代碼報(bào)錯(cuò):Missing or invalid credentials.
9.?git clone報(bào)錯(cuò):SSL certificate problem: self signed certificate in certificate chain
10.?openEuler系統(tǒng)中修改系統(tǒng)時(shí)間
搜索
最新隨筆
1.?dnf安裝失敗
2.?RK3588設(shè)備中運(yùn)行可執(zhí)行程序報(bào)錯(cuò):error while loading shared libraries: librknnrt.so: cannot open shared object file:
3.?wget下載報(bào)錯(cuò):The certificate of ‘www.python.org’ is not trusted.
4.?執(zhí)行torch.load(模型名稱, map_location='cpu')報(bào)錯(cuò):from torchvision.transforms.functional_tensor import rgb_to_grayscale
5.?pip安裝basicsr報(bào)錯(cuò):To fix this you could try to:
6.?cmake文件中D_GLIBCXX_USE_CXX11_ABI=0,導(dǎo)致無(wú)法到入第三方庫(kù)libjsoncpp.so
7.?鏈接libjsoncpp.a時(shí)報(bào)錯(cuò):which may bind externally can not be used when making a shared object; recompile with -fPIC
8.?vs code中g(shù)it push代碼報(bào)錯(cuò):Missing or invalid credentials.
9.?git clone報(bào)錯(cuò):SSL certificate problem: self signed certificate in certificate chain
10.?openEuler系統(tǒng)中修改系統(tǒng)時(shí)間
最新評(píng)論
1.?re: DirectUI Lib XML編寫說(shuō)明
這個(gè)不錯(cuò),很有用。
--dictbox
2.?re: MFC:為CListCtrl添加背景圖片[未登錄](méi)
沒(méi)用
--123
3.?re: DirectUI Lib XML編寫說(shuō)明[未登錄](méi)
很好,對(duì)于我這樣的初學(xué)者很用幫助,謝謝樓主
--king
4.?re: WindowXP下PHP5開(kāi)發(fā)環(huán)境配置
謝謝樓主分享,已經(jīng)按樓主的方法配置成功
--bbreay
5.?re: error C2220: 警告被視為錯(cuò)誤 - 沒(méi)有生成“object”文件
你好,我用的是vs2012,沒(méi)有你說(shuō)的“選擇該cpp”,如:
--coco
閱讀排行榜
1.?protobuf使用方法(9416)
2.?執(zhí)行pip install報(bào)錯(cuò): 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(8960)
3.?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](8625)
4.?編譯cmake報(bào)錯(cuò):Cannot find a C++ compiler that supports both C++11 and the specified C++ flags.(8205)
5.?把python3的版本從3.6升級(jí)到3.10(7235)
評(píng)論排行榜
1.?網(wǎng)游服務(wù)器通信架構(gòu)的設(shè)計(jì)(3)
2.?公司散伙啦。杯具!反思!(3)
3.?Ubuntu9.10 VI下方向鍵變成ABCD的解決辦法(3)
4.?kosmix,又一個(gè)開(kāi)源的類似GFS的分布式文件系統(tǒng)(2)
5.?DirectUI Lib XML編寫說(shuō)明(2)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 小王
无码国内精品久久人妻
|
无码人妻久久一区二区三区
|
亚洲综合伊人久久综合
|
色欲综合久久中文字幕网
|
蜜臀av性久久久久蜜臀aⅴ
|
国产精品九九久久免费视频
|
国产精品久久久久久福利69堂
|
久久亚洲精精品中文字幕
|
久久精品国产亚洲综合色
|
伊人色综合九久久天天蜜桃
|
久久精品一区二区国产
|
久久97久久97精品免视看秋霞
|
欧美日韩久久中文字幕
|
国内精品久久久久久久久电影网
|
国产免费久久精品99re丫y
|
久久99精品国产
|
伊人久久大香线蕉综合5g
|
99久久夜色精品国产网站
|
精品久久久久香蕉网
|
欧美精品久久久久久久自慰
|
国产精品久久久久一区二区三区
|
亚洲av日韩精品久久久久久a
|
色妞色综合久久夜夜
|
亚洲国产精品久久久久婷婷软件
|
久久狠狠爱亚洲综合影院
|
国产香蕉久久精品综合网
|
香蕉久久夜色精品国产小说
|
久久婷婷激情综合色综合俺也去
|
国产精品熟女福利久久AV
|
www.久久热.com
|
国产高潮国产高潮久久久91
|
国产精品无码久久久久久
|
亚洲天堂久久久
|
少妇久久久久久被弄到高潮
|
久久高清一级毛片
|
99久久精品国产毛片
|
av国内精品久久久久影院
|
久久91综合国产91久久精品
|
97久久精品无码一区二区
|
国产亚洲欧美精品久久久
|
久久丫精品国产亚洲av
|