}
};
classCommand
{
public:
Command()
{
}
~Command()
{
}
virtualvoidexecute()=0;
virtualvoidundo()=0;
virtualstringgetClass()=0;
};
classNoCommand:
publicCommand
{
public:
NoCommand()
{
}
~NoCommand()
{
}
voidexecute()
{
}
voidundo()
{
}
stringgetClass()
{
}
};
classLightOnCommand:
publicCommand
{
public:
Light*light;
LightOnCommand(Light*l)
{
light=l;
}
voidexecute()
{
light->on();
}
voidundo()
{
light->off();
}
stringgetClass()
{
return"LightOnCommand";
}
~LightOnCommand()
{
}
};
classLightOffCommand:
publicCommand
{
public:
Light*light;
LightOffCommand(Light*l)
{
light=l;
}
voidexecute()
{
light->off();
}
voidundo()
{
light->on();
}
stringgetClass()
{
return"LightOffCommand";
}
~LightOffCommand()
{
}
};
classCeilingFanOnCommand:
publicCommand
{
public:
CeilingFan*fan;
CeilingFanOnCommand(CeilingFan*f)
{
fan=f;
}
voidexecute()
{
fan->on();
}
voidundo()
{
fan->off();
}
stringgetClass()
{
return"CeilingFanOnCommand";
}
~CeilingFanOnCommand()
{
}
};
classCeilingFanOffCommand:
publicCommand
{
public:
CeilingFan*fan;
CeilingFanOffCommand(CeilingFan*f)
{
fan=f;
}
voidexecute()
{
fan->off();
}
voidundo()
{
fan->on();
}
stringgetClass()
{
return"CeilingFanOffCommand";
}
~CeilingFanOffCommand()
{
}
};
classCeilingFanHighCommand:
publicCommand
{
public:
CeilingFan*fan;
intprevSpeed;
CeilingFanHighCommand(CeilingFan*f)
{
fan=f;
}
voidexecute()
{
prevSpeed=fan->getSpeed();
fan->high();
}
voidundo()
{
if(prevSpeed==fan->HIGH){
fan->high();
}
elseif(prevSpeed==fan->MEDIUM){
fan->medium();
}
elseif(prevSpeed==fan->LOW){
fan->low();
}
}
stringgetClass()
{
return"CeilingFanHighCommand";
}
~CeilingFanHighCommand()
{
}
};
classGarageDoorUpCommand:
publicCommand
{
public:
GarageDoor*door;
GarageDoorUpCommand(GarageDoor*d)
{
door=d;
}
voidexecute()
{
door->up();
}
voidundo()
{
door->down();
}
stringgetClass()
{
return"GarageDoorUpCommand";
}
~GarageDoorUpCommand()
{
}
};
classGarageDoorDownCommand:
publicCommand
{
public:
GarageDoor*door;
GarageDoorDownCommand(GarageDoor*d)
{
door=d;
}
voidexecute()
{
door->down();
}
voidundo()
{
door->up();
}
stringgetClass()
{
return"GarageDoorDownCommand";
}
~GarageDoorDownCommand()
{
}
};
classStereoOnWithCDCommand:
publicCommand
{
public:
Stereo*stereo;
StereoOnWithCDCommand(Stereo*s)
{
stereo=s;
}
voidexecute()
{
stereo->on();
stereo->setCD();
stereo->setVolume(11);
}
voidundo()
{
stereo->off();
}
stringgetClass()
{
return"StereoOnWithCDCommand";
}
~StereoOnWithCDCommand()
{
}
};
classStereoOffCommand:
publicCommand
{
public:
Stereo*stereo;
StereoOffCommand(Stereo*s)
{
stereo=s;
}
voidexecute()
{
stereo->off();
}
voidundo()
{
stereo->on();
stereo->setCD();
stereo->setVolume(11);
}
stringgetClass()
{
return"StereoOffCommand";
}
~StereoOffCommand()
{
}
};
classRemoteControl
{
public:
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;
for(inti=0;i<7;i++)
{
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]!
=NULL){
offCommands[slot]->execute();
undoCommand=offCommands[slot];
}
}
voidundoButtonWasPushed()
{
undoCommand->undo();
}
voidtoString()
{
cout<<"\n------RemoteControl--------\n";
for(inti=0;ilength();i++)
{
cout<<"[slot"<getClass()<<""<getClass()<<"\n";
}
}
};
intmain()
{
RemoteControl*remoteControl=newRemoteControl();
//所有装置创建在合适的位置
Light*livingRoomLight=newLight("LivingRoom");
Light*kitchenLight=newLight("Kitchen");
CeilingFan*ceilingFan=newCeilingFan("LivingRoom");
GarageDoor*garageDoor=newGarageDoor("");
Stereo*stereo=newStereo("LivingRoom");
//创建所有电灯命令对象
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);
remoteControl->setCommand(1,kitchenLightOn,kitchenLightOff);
remoteControl->setCommand(2,ceilingFanOn,ceilingFanOff);
remoteControl->setCommand(3,stereoOnWithCD,stereoOff);
//用toString()方法打印遥控器每个插槽指定的命令
remoteControl->toString();
//按下每个插槽开与关命令
remoteControl->onButtonWasPushed(0);
remoteControl->offButtonWasPushed(0);
remoteControl->onButtonWasPushed
(1);
remoteControl->offButtonWasPushed
(1);
remoteControl->onButtonWasPushed
(2);
remoteControl->offButtonWasPushed
(2);
remoteControl->onButtonWasPushed(3);
remoteControl->offButtonWasPushed(3);
remoteControl->undoButtonWasPushed();
return0;
}