Headfirst设计模式的C++实现——命令模式(Command)
先看如果不用命令模式的实现:
light.h
#ifndef _LIGHT_H_
#define _LIGHT_H #include <iostream> class LIGHT {
public:
void on() { std::cout << "light is on" << std::endl; }
void off() { std::cout << "light is off" << std::endl; }
}; #endif
tv.h
#ifndef _TV_H_
#define _TV_H_ #include <iostream> class TV {
public:
void open() { std::cout << "TV is opened" << std::endl; }
void close() { std::cout << "TV is closed" << std::endl; }
};
#endif
remote.h
#ifndef _REMOTE_H_
#define _REMOTE_H_ #include <string>
#include <iostream>
#include "tv.h"
#include "light.h" class REMOTE {
public:
void set(int slot, const std::string& device) {
if ( slot < MAX_SLOT_NUM ) {
all_device[slot] = device;
}
} void press_on_button(int slot) {
if ( slot >= MAX_SLOT_NUM ) {
return;
}
if ( all_device[slot] == "" ) {
std::cout << "slot " << slot << " is empty" << std::endl;
}
else {
if ( "TV" == all_device[slot] ) {
TV tv;
tv.open();
}
else if ( "LIGHT" == all_device[slot] ) {
LIGHT light;
light.on();
}
else {
std::cout << "invalid type" << std::endl;
}
}
} void press_off_button(int slot) {
if ( slot >= MAX_SLOT_NUM ) {
return;
}
if ( all_device[slot] == "" ) {
std::cout << "slot " << slot << " is empty" << std::endl;
}
else {
if ( "TV" == all_device[slot] ) {
TV tv;
tv.close();
}
else if ( "LIGHT" == all_device[slot] ) {
LIGHT light;
light.off();
}
else {
std::cout << "invalid type" << std::endl;
} }
}
private:
const static int MAX_SLOT_NUM = ;
std::string all_device[MAX_SLOT_NUM];
}; #endif
main.cpp
#include "remote.h"
int main() {
REMOTE remote;
remote.set(, "TV");
remote.set(, "LIGHT");
remote.press_on_button();
remote.press_off_button();
remote.press_on_button();
}
再看看使用命令模式的实现
light.h和tv.h不变
command.h
#ifndef _COMMAND_H_
#define _COMMAND_H_ class COMMAND {
public:
virtual void execute() = ;
}; #endif
light_on_command.h
#ifndef _LIGHT_ON_COMMAND_H_
#define _LIGHT_ON_COMMAND_H_ #include "command.h"
#include "light.h" class LIGHT_ON_COMMAND : public COMMAND {
private:
LIGHT &light;
public:
LIGHT_ON_COMMAND( LIGHT &_light ) : light(_light) {}
void execute() { light.on(); }
}; #endif
light_off_command.h
#ifndef _LIGHT_OFF_COMMAND_H_
#define _LIGHT_OFF_COMMAND_H_ #include "command.h"
#include "light.h" class LIGHT_OFF_COMMAND : public COMMAND {
private:
LIGHT &light;
public:
LIGHT_OFF_COMMAND( LIGHT &_light ) : light(_light) {}
void execute() { light.off(); }
}; #endif
tv_on_command.h
#ifndef _TV_ON_COMMAND_H_
#define _Tv_ON_COMMAND_H_ #include "command.h"
#include "tv.h" class TV_ON_COMMAND : public COMMAND {
private:
TV &tv;
public:
TV_ON_COMMAND( TV&_tv) : tv(_tv) {}
void execute() { tv.open(); }
}; #endif
tv_off_command.h
#ifndef _TV_OFF_COMMAND_H_
#define _TV_OFF_COMMAND_H_ #include "command.h"
#include "tv.h" class TV_OFF_COMMAND : public COMMAND {
private:
TV &tv;
public:
TV_OFF_COMMAND( TV&_tv) : tv(_tv) {}
void execute() { tv.close(); }
}; #endif
remote.h
#ifndef _REMOTE_H_
#define _REMOTE_H_ #include "command.h"
#include <iostream> class REMOTE {
public:
REMOTE() {
for ( int i = ; i < MAX_SLOT_NUM; i++ ) {
on_commands[i] = NULL;
off_commands[i] = NULL;
}
} void set(int slot, COMMAND& on_command, COMMAND& off_command) {
if ( slot < MAX_SLOT_NUM ) {
on_commands[slot] = &on_command;
off_commands[slot] = &off_command;
}
} void press_on_button(int slot) {
if ( slot < MAX_SLOT_NUM ) {
if ( NULL != on_commands[slot] ) {
on_commands[slot]->execute();
}
else {
std::cout << "slot " << slot << " is empty" << std::endl;
}
}
} void press_off_button(int slot) {
if ( slot < MAX_SLOT_NUM ) {
if ( NULL != off_commands[slot] ) {
off_commands[slot]->execute();
}
else {
std::cout << "slot " << slot << " is empty" << std::endl;
}
}
}
private:
const static int MAX_SLOT_NUM = ;
COMMAND *on_commands[MAX_SLOT_NUM];
COMMAND *off_commands[MAX_SLOT_NUM];
}; #endif
main.cpp
#include "remote.h"
#include "tv_on_command.h"
#include "tv_off_command.h"
#include "light_on_command.h"
#include "light_off_command.h" int main() {
REMOTE remote; TV tv;
TV_ON_COMMAND tv_on_command(tv);
TV_OFF_COMMAND tv_off_command(tv);
remote.set(, tv_on_command, tv_off_command); LIGHT light;
LIGHT_ON_COMMAND light_on_command(light);
LIGHT_OFF_COMMAND light_off_command(light);
remote.set(, light_on_command, light_off_command); remote.press_on_button();
remote.press_off_button();
remote.press_on_button();
}
Headfirst设计模式的C++实现——命令模式(Command)的更多相关文章
- headfirst设计模式(7)—命令模式
一.前言 什么是命令模式? 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之RemoteControlTest[转]
1 2{<HeadFirst设计模式>之命令模式 } 3{ 本单元中的类为命令的接收者 } 4{ 编译工具 :Delphi7.0 } 5{ 联 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest[转]
命令模式可以很轻松的实现撤销(Undo)功能. 命令的接受者: 1unit uReceiveObject; 2 3interface 4 5type 6 TLight = class(T ...
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
设计模式 ( 十三 ) 命令模式Command(对象行为型) 1.概述 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需 ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
- 命令模式 Command 行为型 设计模式(十八)
命令模式(Command) 请分析上图中这条命令的涉及到的角色以及执行过程,一种可能的理解方式是这样子的: 涉及角色为:大狗子和大狗子他妈 过程为:大狗子他妈角色 调用 大狗子的“回家吃饭”方法 引子 ...
- Java设计模式(22)命令模式(Command模式)
Command模式是最让我疑惑的一个模式,我在阅读了很多代码后,才感觉隐约掌握其大概原理,我认为理解设计模式最主要是掌握起原理构造,这样才对自己实际编程有指导作用.Command模式实际上不是个很具体 ...
- Java 设计模式系列(十四)命令模式(Command)
Java 设计模式系列(十四)命令模式(Command) 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复 ...
- 设计模式 - 命令模式(command pattern) 多命令 具体解释
命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...
随机推荐
- Codeforces 295C Greg and Friends
BFS+DP.dp[i][j][0]表示有i个50kg,j个100kg的人在左岸,dp[i][j][1]表示有i个50kg,j个100kg的人在右岸.用BFS求最短路的时候记录到达该状态的可能情况. ...
- JavaScript- 图片无缝滚动
图片向上.向下.向左.向右不间断无缝滚动. 图片向下滚动: <div id="colee" style="overflow: hidden; height: 253 ...
- TDD中的迭代
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:TDD中的迭代.
- lettCode-Array
1 Remove Element lintcode-172 描述: 删相同元素,反现有长度 记忆:标不同元素,反标记值 public int removeElement(int[] a, i ...
- [转载](iPhone开发)Bundle Display Name 改为中文。ap
原文地址:(iPhone开发)Bundle Display Name 改为中文.app显示为中文作者:小新 要先在info的Localizations加入Chinese(zh-Hant) 再進到inf ...
- SQL Server将一列的多行内容拼接成一行的问题讨论
转自http://blog.csdn.net/rolamao/article/details/7745972 昨天遇到一个SQL Server的问题:需要写一个储存过程来处理几个表中的数据,最后问题出 ...
- ListView视图缓存错位问题
由于之前写Scroller应用:ListView滑动删除遇到Item视图错位问题,观察发现第1item位置改变后,第1+10的item布局也跟着改变.假设使用ScrollView+ListView,把 ...
- [Webpack 2] Import a non-ES6 module with Webpack
When you have a dependency that does not export itself properly, you can use the exports-loader to f ...
- Android用悬浮按钮实现翻页效果
今天给大家分享下自己用悬浮按钮点击实现翻页效果的例子. 首先,一个按钮要实现悬浮,就要用到系统顶级窗口相关的WindowManager,WindowManager.LayoutParams.那么在An ...
- 5天学会jaxws-webservice编程第一天
前言: 随着近几年来,SOA,EAI等架构体系的日渐成熟,Webservice越来越炽手可热,尤其是在企业做异质平台整合时成为了首选的技术. Java的Webservice技术更是层出不穷,比較流行的 ...