设计模式入门,命令模式,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 ...
随机推荐
- Codeforces 464E. The Classic Problem
题目大意 给定一张$n$个点, $m$条边的无向图,求$S$ 到$T$的最短路,其中边权都是$2^k$的形式$n,m,k<=10^5$,结果对$10^9+7$取模 题解 大佬好厉害 跑一边dij ...
- 移动 UX 设计:如何设计推送通知
这个问题你一定想过,在移动用户体验设计领域中,如何设计好一条简单的推送通知. 你注意过么,每天从不同的 App 上收到的大量的推送通知与提醒,这些通知里有多少你真的有兴趣? 每天,用户对各种没用的通知 ...
- Android来电、去电监听
Android手机中添加手机来电的状态,使用PhoneStateListener来监听. TelephonyManager telephonyManager = (TelephonyManager) ...
- thinkphp3.2----设置静态缓存
开启静态缓存后,页面刷新时获取的是静态页面,控制器增加输出内容时页面还是一样,除非超过缓存时间或html结构发生变化才重新生成页面缓存 1.定义静态缓存目录 define("HTML_PAT ...
- sql 列集合
STUFF((SELECT ','+CAST( TYZ_Bh as varchar(10)) FROM #1 where 片区划分='江东' for xml path('')),1,1,'')
- Git密钥
SSH keys SSH key 可以让你在你的电脑和Code服务器之间建立安全的加密连接. 先执行以下语句来判断是否已经存在本地公钥: 1. cat ~/.ssh/id_rsa.pub 如果你 ...
- 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest
题目传送门 只打了三个小时. A. Broadcast Stations B. Connect3 补题:zz 题解:因为格子是4*4的,而且每次落子的位置最多是只有四个,再加上剪枝,情况不会很多,直接 ...
- 股神 C++
题目描述 有股神吗? 有,小赛就是! 经过严密的计算,小赛买了一支股票,他知道从他买股票的那天开始,股票会有以下变化:第一天不变,以后涨一天,跌一天,涨两天,跌一天,涨三天,跌一天...依此类推. 为 ...
- es第二篇:Document APIs
文档CRUD API分为单文档API和多文档API.这些API的索引名参数既可以是一个真正的索引的名称,也可以是某个索引的别名alias. 单文档API有:Index API.Get API.Dele ...
- SC OpenService 失败5:拒绝访问
当我们在cmd里使用 sc delete 服务名 ,来删除服务的时候,报错误,SC OpenService 失败5:拒绝访问. 这似乎是因为权限不够,解决方法. 首先,我们必须先取得管理员权限,以 ...