6命令模式解析.docx

上传人:b****6 文档编号:7843582 上传时间:2023-01-26 格式:DOCX 页数:17 大小:27.13KB
下载 相关 举报
6命令模式解析.docx_第1页
第1页 / 共17页
6命令模式解析.docx_第2页
第2页 / 共17页
6命令模式解析.docx_第3页
第3页 / 共17页
6命令模式解析.docx_第4页
第4页 / 共17页
6命令模式解析.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

6命令模式解析.docx

《6命令模式解析.docx》由会员分享,可在线阅读,更多相关《6命令模式解析.docx(17页珍藏版)》请在冰豆网上搜索。

6命令模式解析.docx

6命令模式解析

命令模式

类图:

代码:

#include

usingnamespacestd;

classLight

{

public:

Light(strings)

{

cout<<"LightOn"<

}

~Light()

{

}

voidon()

{

cout<<"LightOn"<

}

voidoff()

{

cout<<"LightOff"<

}

};

classCeilingFan

{

public:

staticconstintHIGH=3;

staticconstintMEDIUM=2;

staticconstintLOW=1;

staticconstintOFF=0;

stringlocation;

intspeed;

CeilingFan(strings)

{

location=s;

speed=OFF;

cout<<"CeilingFanOn"<

}

~CeilingFan()

{

}

voidon()

{

cout<<"CeilingFanOn"<

}

voidoff()

{

speed=OFF;

cout<<"CeilingFanOff"<

}

voidhigh()

{

speed=HIGH;

}

voidmedium()

{

speed=MEDIUM;

}

voidlow()

{

speed=LOW;

}

intgetSpeed()

{

returnspeed;

}

};

classGarageDoor

{

public:

GarageDoor(strings)

{

cout<<"GarageDoorOn"<

}

~GarageDoor()

{

}

voidup()

{

cout<<"GarageDoorUp"<

}

voiddown()

{

cout<<"GarageDoordown"<

}

};

classStereo

{

public:

Stereo(strings)

{

cout<<"StereoOn"<

}

~Stereo()

{

}

voidon()

{

cout<<"StereoOn"<

}

voidoff()

{

cout<<"StereoOff"<

}

voidsetCD()

{

cout<<"StereoCD"<

}

voidsetVolume(intn)

{

cout<<"StereoVolume"<

}

};

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;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1