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 ...
随机推荐
- Configuring Active Directory Federation Services 2.0 (配置 adfs 2.0) -摘自网络
Active Directory Federation Services (AD FS) 2.0 makes it possible to deploy a federation server and ...
- 用g++ 编译 ffmpeg 编译出现 error: 'UINT64_C' was not declared in this scope 或 missing -D__STDC_CONSTANT_MACROS
在 libavutil/common.h 下 添加如下,即可解决 #ifdef __cplusplus#define __STDC_CONSTANT_MACROS#ifdef _STDINT_H#un ...
- openstack 镜像自动扩容 resize拉伸
The simplest way to support this in your image is to install the cloud-utils package (contains the g ...
- 10个强大的Apache开源模块
1.单点登录模块 LemonLDAP LemonLdap可以很棒地实现Apache的SSO功能,并且可以处理超过 20 万的用户请求.LemonLdap支持Java, PHP, .Net, Perl, ...
- poj1873 The Fortified Forest 凸包+枚举 水题
/* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...
- 包含块、层叠上下文、BFC
包含块 什么是包含块?简单来说,就是决定一个元素大小和定位的元素.一个元素会为它的内部元素创建包含块,但也不能说元素的包含块就是它的父元素: 1.position:fixed 的元素 包含块是当前可视 ...
- Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法
java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind 此异常的原因是服务器端口被占用 所以解决办法是: 一 ...
- android103 内容观察者
#内容观察者 * 通过内容提供者可以访问到数据库,当数据库数据改变时,内容提供者会发出通知,在内容提供者的uri上注册一个内容观察者,就可以收到数据改变的通知,类似于广播接受者,但是他不是广播. cr ...
- iOS9适配
一.App Transport Security xcode7安装后,你会发现ios9之后后默认所有http请求都无法继续有效,但是基于现状,我们并不能这么快改成https请求,所以基本上大多数app ...
- checkbox组件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...