设计模式入门,命令模式,c++代码实现
// test06.cpp : Defines the entry point for the console application.
//
//设计模式第5章 命令模式
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Command
{
public:
virtual void execute(){}
};
class Light
{
string position;
public:
Light(string strPos)
{
position = strPos;
}
public:
void on()
{
cout<<position<<"Light is on"<<endl;
}
void off()
{
cout<<position<<"Light is off"<<endl;
}
};
class CeilingFan
{
string position;
public:
CeilingFan(string strPos)
{
position = strPos;
}
public:
void high()
{
cout<<position<<"ceiling fan is on high"<<endl;
}
void medium()
{
cout<<position<<"ceiling fan is on medium"<<endl;
}
void off()
{
cout<<position<<"ceiling fan is off"<<endl;
}
};
class GarageDoor
{
string position;
public:
GarageDoor(string strPos)
{
position = strPos;
}
public:
void up()
{
cout<<position<<"garage door is up"<<endl;
}
void down()
{
cout<<position<<"garage door is down"<<endl;
}
};
class Stereo
{
string position;
public:
Stereo(string strPos)
{
position = strPos;
}
public:
void on()
{
cout<<position<<"stereo is on"<<endl;
}
void off()
{
cout<<position<<"stereo is off"<<endl;
}
void setCD()
{
cout<<position<<"stereo is set for CD"<<endl;
}
void setVolume(int volume)
{
cout<<position<<"stereo volume set to "<<volume<<endl;
}
};
class LightOnCommand : public Command
{
Light* light;
public:
LightOnCommand(Light* light)
{
this->light = light;
}
void execute()
{
light->on();
}
};
class LightOffCommand:public Command
{
Light* light;
public:
LightOffCommand(Light* light)
{
this->light = light;
}
void execute()
{
light->off();
}
};
class CeilingFanOnCommand:public Command
{
CeilingFan* ceilingfan;
public:
CeilingFanOnCommand(CeilingFan* pCeilingFan)
{
ceilingfan = pCeilingFan;
}
void execute()
{
ceilingfan->high();
}
};
class CeilingFanOffCommand:public Command
{
CeilingFan* ceilingfan;
public:
CeilingFanOffCommand(CeilingFan* pCeilingFan)
{
ceilingfan = pCeilingFan;
}
void execute()
{
ceilingfan->off();
}
};
class GarageDoorUpCommand:public Command
{
GarageDoor* garagedoor;
public:
GarageDoorUpCommand(GarageDoor* pGaragedoor)
{
garagedoor = pGaragedoor;
}
void execute()
{
garagedoor->up();
}
};
class GarageDoorDownCommand:public Command
{
GarageDoor* garagedoor;
public:
GarageDoorDownCommand(GarageDoor* pGaragedoor)
{
garagedoor = pGaragedoor;
}
void execute()
{
garagedoor->down();
}
};
class StereoOnWithCDCommand:public Command
{
Stereo* stereo;
public:
StereoOnWithCDCommand(Stereo* pStereo)
{
stereo = pStereo;
}
void execute()
{
stereo->on();
stereo->setCD();
stereo->setVolume(11);
}
};
class StereoOffCommand:public Command
{
Stereo* stereo;
public:
StereoOffCommand(Stereo* pStereo)
{
stereo = pStereo;
}
void execute()
{
stereo->off();
}
};
class NoCommand:public Command
{
public:
void execute(){}
};
class RemoteControl
{
Command** onCommands;
Command** offCommands;
public:
RemoteControl()
{
onCommands = new Command* [7];
offCommands = new Command* [7];
Command* noCommand = new NoCommand();
for(int i = 0; i< 7; i++)
{
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
}
void setCommand(int slot, Command* onCommand, Command* offCommand)
{
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
void onButtonWasPushed(int slot)
{
onCommands[slot]->execute();
}
void offButtonWasPushed(int slot)
{
offCommands[slot]->execute();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
RemoteControl remoteControl;
Light livingroomLight("Living Room");
Light kitchenLight("Kitchen");
CeilingFan ceilingfan("Livint Room");
GarageDoor garadoor("");
Stereo stereo("Livint Room");
LightOnCommand* livingroomLightOn = new LightOnCommand(&livingroomLight);
LightOffCommand* livintroomLightOff = new LightOffCommand(&livingroomLight);
LightOnCommand* kitchenLightOn = new LightOnCommand(&kitchenLight);
LightOffCommand* kitchenLightOff = new LightOffCommand(&kitchenLight);
CeilingFanOnCommand* ceilingFanOn = new CeilingFanOnCommand(&ceilingfan);
CeilingFanOffCommand* ceilingFanOff = new CeilingFanOffCommand(&ceilingfan);
GarageDoorUpCommand* garageDoorUp = new GarageDoorUpCommand(&garadoor);
GarageDoorDownCommand* garageDoorDown = new GarageDoorDownCommand(&garadoor);
StereoOnWithCDCommand* stereoOnWithCD = new StereoOnWithCDCommand(&stereo);
StereoOffCommand* stereoOff = new StereoOffCommand(&stereo);
remoteControl.setCommand(0,livingroomLightOn,livintroomLightOff);
remoteControl.setCommand(1,kitchenLightOn,kitchenLightOff);
remoteControl.setCommand(2,ceilingFanOn,ceilingFanOff);
remoteControl.setCommand(3,stereoOnWithCD,stereoOff);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(2);
remoteControl.onButtonWasPushed(3);
remoteControl.offButtonWasPushed(3);
return 0;
}
设计模式入门,命令模式,c++代码实现的更多相关文章
- 设计模式入门,策略模式,c++代码实现
// test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...
- 设计模式入门,单件模式,c++代码实现
// test05.cpp : Defines the entry point for the console application.// #include "stdafx.h" ...
- 设计模式入门,工厂模式,c++代码实现
// test04.cpp : Defines the entry point for the console application.////设计模式第4章 工厂模式#include "s ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
- 面向对象设计模式_命令模式(Command)解读
在.Net框架中很多对象的方法中都会有Invoke方法,这种方法的设计实际是用了设计模式的命令模式, 模式图如下 其核心思路是将Client 向Receiver发送的命令行为进行抽象(ICommand ...
- 折腾Java设计模式之命令模式
博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...
- 用Java 8 Lambda表达式实现设计模式:命令模式
在这篇博客里,我将说明如何在使用 Java 8 Lambda表达式 的函数式编程方式 时实现 命令 设计模式 .命令模式的目标是将请求封装成一个对象,从对客户端的不同类型请求,例如队列或日志请求参数化 ...
- 设计模式之命令模式-JS
理解命令模式 假设有一个快餐店,而我是该餐厅的点餐服务员,那么我一天的工作应该是这样的:当某位客人点餐或者打来订餐电话后,我会把他的需求都写在清单上,然后交给厨房,客人不用关心是哪些厨师帮他炒菜.我们 ...
- python设计模式之命令模式
python设计模式之命令模式 现在多数应用都有撤销操作.虽然难以想象,但在很多年里,任何软件中确实都不存在撤销操作.撤销操作是在1974年引入的,但Fortran和Lisp分别早在1957年和195 ...
随机推荐
- VMware虚拟机中如何配置静态IP
我们首先说一下VMware的几个虚拟设备 VMnet0:用于虚拟桥接网络下的虚拟交换机 VMnet1:用于虚拟Host-Only网络下的虚拟交换机 VMnet8:用于虚拟NAT网络下的虚拟交换机 VM ...
- [Swift]Swift的常用内置函数
内置函数:在Swift中不需要导入任何模块(如UIKit等)或者引用任何类就可以使用的函数.Swift预先定义的函数//快捷键Control+⌘+Space来开启Emoji字符输入 1.断言asser ...
- java操作AWS S3一些坑记录
1,aws sdk jar版本不一致问题 一开始我在pom.xml中只配置了如下aws-java-sdk-s3 <!-- https://mvnrepository.com/artifact/c ...
- ubuntu15.04下安装jdk8
前几天手贱,删掉了ubuntu自带的java,最后安装时遇到了Picked up JAVA_TOOL_OPTIONS的问题,经过网上各种找,终于被我弄成功了.下面将经验下载下面供大家方便: jdk8的 ...
- mysql5.7解压版版安装步骤详情
mysql有安装版和解压版之分: 安装版:以msi结尾的,这种版本优点是安装便捷,全是傻瓜式的下一步:缺点是会不自觉的安装一些我们根本不需要的组件. 解压版:以zip或者其他压缩格式结尾的,这种版本虽 ...
- To 高一
Linux 坑待填 Special Judge 什么是 Special Judge?有的题目会让你输出任意一个解即可AC,或者是让你输出部分答案能取得本测试点部分得分,或者是按照方案的优秀程度给你分, ...
- Linux命令-关机命令:shutdown
shutdown -h now 现在马上关机 shutdown -h : 晚上8:30定时关机shutdown -r now 现在马上重起shutdown -r 20:30 晚上8:30定时重起 ca ...
- 吴裕雄 python 机器学习——K均值聚类KMeans模型
import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...
- FPGA基础学习(9) -- 复位设计
目录 1. 常见问题 2. 常见的复位方式 3. 合理的复位设计 3.1 复位电平 3.2 异步复位同步化 3.3 恰到好处的复位 4. 补充 4.1 所谓的上电初始化 参考文献 一开始接触到FPGA ...
- wordcloud词云--可视化
import jieba import wordcloud f = open("新时代中国特色社会主义.txt", "r", encoding="gb ...