6命令模式解析Word文档下载推荐.docx
《6命令模式解析Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《6命令模式解析Word文档下载推荐.docx(17页珍藏版)》请在冰豆网上搜索。
};
classCeilingFan
staticconstintHIGH=3;
staticconstintMEDIUM=2;
staticconstintLOW=1;
staticconstintOFF=0;
stringlocation;
intspeed;
CeilingFan(strings)
location=s;
speed=OFF;
CeilingFanOn"
location<
~CeilingFan()
CeilingFanOn"
speed=OFF;
CeilingFanOff"
voidhigh()
speed=HIGH;
voidmedium()
speed=MEDIUM;
voidlow()
speed=LOW;
intgetSpeed()
returnspeed;
classGarageDoor
GarageDoor(strings)
GarageDoorOn"
~GarageDoor()
voidup()
GarageDoorUp"
voiddown()
GarageDoordown"
classStereo
Stereo(strings)
StereoOn"
~Stereo()
StereoOn"
StereoOff"
voidsetCD()
StereoCD"
voidsetVolume(intn)
StereoVolume"
n<
classCommand
Command()
~Command()
virtualvoidexecute()=0;
virtualvoidundo()=0;
virtualstringgetClass()=0;
classNoCommand:
publicCommand
NoCommand()
~NoCommand()
voidexecute()
voidundo()
stringgetClass()
classLightOnCommand:
Light*light;
LightOnCommand(Light*l)
light=l;
light->
on();
off();
return"
LightOnCommand"
;
~LightOnCommand()
classLightOffCommand:
LightOffCommand(Light*l)
LightOffCommand"
~LightOffCommand()
classCeilingFanOnCommand:
CeilingFan*fan;
CeilingFanOnCommand(CeilingFan*f)
fan=f;
fan->
CeilingFanOnCommand"
~CeilingFanOnCommand()
classCeilingFanOffCommand:
CeilingFanOffCommand(CeilingFan*f)
CeilingFanOffCommand"
~CeilingFanOffCommand()
classCeilingFanHighCommand:
intprevSpeed;
CeilingFanHighCommand(CeilingFan*f)
prevSpeed=fan->
getSpeed();
high();
if(prevSpeed==fan->
HIGH){
elseif(prevSpeed==fan->
MEDIUM){
medium();
LOW){
low();
CeilingFanHighCommand"
~CeilingFanHighCommand()
classGarageDoorUpCommand:
GarageDoor*door;
GarageDoorUpCommand(GarageDoor*d)
door=d;
door->
up();
down();
GarageDoorUpCommand"
~GarageDoorUpCommand()
classGarageDoorDownCommand:
GarageDoorDownCommand(GarageDoor*d)
GarageDoorDownCommand"
~GarageDoorDownCommand()
classStereoOnWithCDCommand:
Stereo*stereo;
StereoOnWithCDCommand(Stereo*s)
stereo=s;
stereo->
setCD();
setVolume(11);
StereoOnWithCDCommand"
~StereoOnWithCDCommand()
classStereoOffCommand:
StereoOffCommand(Stereo*s)
StereoOffCommand"
~StereoOffCommand()
classRemoteControl
Command*onCommands[7];
Command*offCommands[7];
Command*undoCommand;
Command*noCommand;
RemoteControl()
noCommand=newNoCommand();
for(inti=0;
i<
7;
i++)
onCommands[i]=noCommand;
offCommands[i]=noCommand;
undoCommand=noCommand;
intlength()
intlen=0;
if(onCommands[i]!
=noCommand)
len++;
returnlen;
voidsetCommand(intslot,Command*onCommand,Command*offCommand)
onCommands[slot]=onCommand;
offCommands[slot]=offCommand;
voidonButtonWasPushed(intslot)
if(onCommands[slot]!
=NULL){
onCommands[slot]->
execute();
undoCommand=onCommands[slot];
voidoffButtonWasPushed(intslot)
if(offCommands[slot]!
offCommands[slot]->
undoCommand=offCommands[slot];
voidundoButtonWasPushed()
undoCommand->
undo();
voidtoString()
\n------RemoteControl--------\n"
this->
length();
[slot"
]"
onCommands[i]->
getClass()<
"
offCommands[i]->
\n"
intmain()
RemoteControl*remoteControl=newRemoteControl();
//所有装置创建在合适的位置
Light*livingRoomLight=newLight("
LivingRoom"
);
Light*kitchenLight=newLight("
Kitchen"
CeilingFan*ceilingFan=newCeilingFan("
GarageDoor*garageDoor=newGarageDoor("
Stereo*stereo=newStereo("
//创建所有电灯命令对象
LightOnCommand*livingRoomLightOn=
newLightOnCommand(livingRoomLight);
LightOffCommand*livingRoomLightOff=
newLightOffCommand(livingRoomLight);
LightOnCommand*kitchenLightOn=
newLightOnCommand(kitchenLight);
LightOffCommand*kitchenLightOff=
newLightOffCommand(kitchenLight);
//创建掉扇开与关命令
CeilingFanOnCommand*ceilingFanOn=
newCeilingFanOnCommand(ceilingFan);
CeilingFanOffCommand*ceilingFanOff=
newCeilingFanOffCommand(ceilingFan);
//创建车库门上与下的命令
GarageDoorUpCommand*garageDoorUp=
newGarageDoorUpCommand(garageDoor);
GarageDoorDownCommand*garageDoorDown=
newGarageDoorDownCommand(garageDoor);
//创建音响开与关命令
StereoOnWithCDCommand*stereoOnWithCD=
newStereoOnWithCDCommand(stereo);
StereoOffCommand*stereoOff=
newStereoOffCommand(stereo);
//所有命令加载到遥控器插槽中
remoteControl->
setCommand(0,livingRoomLightOn,livingRoomLightOff);
setCommand(1,kitchenLightOn,kitchenLightOff);
setCommand(2,ceilingFanOn,ceilingFanOff);
setCommand(3,stereoOnWithCD,stereoOff);
//用toString()方法打印遥控器每个插槽指定的命令
toString();
//按下每个插槽开与关命令
onButtonWasPushed(0);
offButtonWasPushed(0);
onButtonWasPushed
(1);
offButtonWasPushed
(1);
onButtonWasPushed
(2);
offButtonWasPushed
(2);
onButtonWasPushed(3);
offButtonWasPushed(3);
undoButtonWasPushed();
return0;
}