命令模式:将请求封装成对象。
C++示例代码如下:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
/*
* CONTENTS: DESIGN PATTERN, COMMAND PATTERN
* AUTHOR: YAO H. WANG
* TIME: 2013-11-2 22:35:35
* EDITION: 1
* LINK: http://blog.csdn.net/yaohwang
*
* ALL RIGHTS RESERVED!
*/
//吊扇
class CeilingFan
{
public:
static const int HIGH = 3;
static const int MEDIUM = 2;
static const int LOW = 1;
static const int OFF = 0;
string location;
int speed;
CeilingFan(string location)
{
this->location = location;
}
void high()
{
speed = HIGH;
cout << location << " ceiling fan is on high" << endl;
}
void medium()
{
speed = MEDIUM;
cout << location << " ceiling fan is on medium" << endl;
}
void low()
{
speed = LOW;
cout << location << " ceiling fan is on low" << endl;
}
void off()
{
speed = OFF;
cout << location << " ceiling fan is off" << endl;
}
int getSpeed()
{
return speed;
}
};
//命令
class Command
{
public:
virtual void execute() = 0;
virtual void undo() = 0;
virtual string getName() = 0;
};
class CeilingFanHighCommand: public Command
{
private:
CeilingFan *ceilingFan;
int prevSpeed;
public:
CeilingFanHighCommand(CeilingFan *ceilingFan)
{
this->ceilingFan = ceilingFan;
}
void execute()
{
prevSpeed = ceilingFan->getSpeed();
ceilingFan->high();
}
void undo()
{
if(CeilingFan::HIGH == prevSpeed)
ceilingFan->high();
else if(CeilingFan::MEDIUM == prevSpeed)
ceilingFan->medium();
else if(CeilingFan::LOW == prevSpeed)
ceilingFan->low();
else if(CeilingFan::OFF == prevSpeed)
ceilingFan->off();
}
string getName()
{
return "CeilingFanHighCommand";
}
};
class CeilingFanMediumCommand: public Command
{
private:
CeilingFan *ceilingFan;
int prevSpeed;
public:
CeilingFanMediumCommand(CeilingFan *ceilingFan)
{
this->ceilingFan = ceilingFan;
}
void execute()
{
prevSpeed = ceilingFan->getSpeed();
ceilingFan->medium();
}
void undo()
{
if(CeilingFan::HIGH == prevSpeed)
ceilingFan->high();
else if(CeilingFan::MEDIUM == prevSpeed)
ceilingFan->medium();
else if(CeilingFan::LOW == prevSpeed)
ceilingFan->low();
else if(CeilingFan::OFF == prevSpeed)
ceilingFan->off();
}
string getName()
{
return "CeilingFanMediumCommand";
}
};
class CeilingFanOffCommand: public Command
{
private:
CeilingFan *ceilingFan;
int prevSpeed;
public:
CeilingFanOffCommand(CeilingFan *ceilingFan)
{
this->ceilingFan = ceilingFan;
}
void execute()
{
prevSpeed = ceilingFan->getSpeed();
ceilingFan->off();
}
void undo()
{
if(CeilingFan::HIGH == prevSpeed)
ceilingFan->high();
else if(CeilingFan::MEDIUM == prevSpeed)
ceilingFan->medium();
else if(CeilingFan::LOW == prevSpeed)
ceilingFan->low();
else if(CeilingFan::OFF == prevSpeed)
ceilingFan->off();
}
string getName()
{
return "CeilingFanOffCommand";
}
};
class NoCommand: public Command
{
public:
void execute()
{
}
void undo()
{
}
string getName()
{
return "NoCommand";
}
};
//遥控器
class RemoteControlWithUndo
{
private:
Command *onCommands[7];
Command *offCommands[7];
Command *undoCommand;
public:
RemoteControlWithUndo()
{
Command *noCommand = new NoCommand();
for(int i = 0; i < 7; ++i)
{
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
undoCommand = noCommand;
}
void setCommand(int slot, Command *onCommand, Command *offCommand)
{
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
void onButtonWasPushed(int slot)
{
onCommands[slot]->execute();
undoCommand = onCommands[slot];
}
void offButtonWasPushed(int slot)
{
offCommands[slot]->execute();
undoCommand = offCommands[slot];
}
void undoButtonWasPushed()
{
undoCommand->undo();
}
void toString()
{
cout << endl << "------ Remote Control -------" << endl;
for(int i = 0; i < 7; ++i)
cout << onCommands[i]->getName() << "\t" << offCommands[i]->getName() << endl;
cout << undoCommand->getName() << endl << endl;
}
};
//测试
void main()
{
RemoteControlWithUndo remoteControl;
CeilingFan *ceilingFan = new CeilingFan("Living Room");
CeilingFanMediumCommand *ceilFanMedium = new CeilingFanMediumCommand(ceilingFan);
CeilingFanHighCommand *ceilFanHigh = new CeilingFanHighCommand(ceilingFan);
CeilingFanOffCommand *ceilFanOff = new CeilingFanOffCommand(ceilingFan);
remoteControl.setCommand(0, ceilFanMedium, ceilFanOff);
remoteControl.setCommand(1, ceilFanHigh, ceilFanOff);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.toString();
remoteControl.undoButtonWasPushed();
remoteControl.onButtonWasPushed(1);
remoteControl.toString();
remoteControl.undoButtonWasPushed();
delete ceilingFan;
delete ceilFanMedium;
delete ceilFanHigh;
delete ceilFanOff;
}