最经典的例子是餐馆点餐菜。服务员用菜单记录,厨师按照菜单做菜。服务员不需要知道菜具体怎么做的。还可以实现日志记录,最后照着菜单付钱。
#include <iostream>
#include <string>
using namespace std;
class Command
{
public :
virtual void execute(){ }
virtual void undo() { }
Command operator = (const Command& command)
{
return command;
}
};
class Light
{
public:
string location;
Light(){}
Light(string location)
{
this->location = location;
}
void off()
{
cout << location << " light if off" << endl;
}
void on()
{
cout << location << " light is on" << endl;
}
};
class LightOnCommand : public Command
{
public:
Light* light;
LightOnCommand(Light* light)
{
this->light = light;
}
virtual void execute()
{
light->on();
}
virtual void undo()
{
light->off();
}
};
class LightOffCommand : public Command
{
public:
Light* light;
LightOffCommand(Light* light)
{
this->light = light;
}
virtual void execute()
{
light->off();
}
virtual void undo()
{
light->on();
}
};
class GarageDoor
{
public:
string location;
GarageDoor(){}
GarageDoor(string location)
{
this->location = location;
}
void up()
{
cout << location << "gara is up" << endl;
}
void down()
{
cout<< location << "gara is down " << endl;
}
void stop()
{
cout << " gara is stopped" << endl;
}
void lightOn()
{
cout << "gara light is on" << endl;
}
void lightOff()
{
cout << "gara light is off" << endl;
}
};
class GarageDoorDownCommand : public Command
{
public:
GarageDoor* garageDoor;
GarageDoorDownCommand(GarageDoor* garageDoor)
{
this->garageDoor = garageDoor;
}
virtual void execute()
{
garageDoor->down();
}
virtual void undo()
{
garageDoor->up();
}
};
class GarageDoorUpCommand : public Command
{
public:
GarageDoor* garageDoor;
GarageDoorUpCommand(GarageDoor* garageDoor)
{
this->garageDoor = garageDoor;
}
virtual void execute()
{
garageDoor->up();
}
virtual void undo()
{
garageDoor->down();
}
};
class NoCommand : public Command
{
public :
virtual void execute()
{
}
};
class RemoteControl
{
public :
Command** onCommands;
Command** offCommands;
Command* undoCommand;
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;
}
undoCommand = noCommand;
}
void setCommand(int slot, Command* onCommand, Command* offCommand)
{
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
void onButtonWasPressed(int slot)
{
onCommands[slot]->execute();
undoCommand = onCommands[slot];
}
void offButtonWasPushed(int slot)
{
offCommands[slot]->execute();
undoCommand = offCommands[slot];
}
void undoButtonWasPressed()
{
undoCommand->undo();
}
};
int main()
{
RemoteControl* remoteControl = new RemoteControl();
Light* livingRoomLight = new Light("living Room");
Light* kitchenLight = new Light("Kitchen");
GarageDoor* garageDoor = new GarageDoor("");
LightOnCommand* livingRoomLightOn = new LightOnCommand(livingRoomLight);
LightOffCommand* livingRoomLightOff = new LightOffCommand(livingRoomLight);
LightOnCommand* kitchenLightOn = new LightOnCommand(kitchenLight);
LightOffCommand* kitchenLightOff = new LightOffCommand(kitchenLight);
GarageDoorUpCommand* garageDoorUp = new GarageDoorUpCommand(garageDoor);
GarageDoorDownCommand* garageDoorDown = new GarageDoorDownCommand(garageDoor);
remoteControl->setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl->setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl->setCommand(2, garageDoorUp, garageDoorDown);
remoteControl->onButtonWasPressed(0);
remoteControl->offButtonWasPushed(0);
remoteControl->onButtonWasPressed(1);
remoteControl->offButtonWasPushed(1);
remoteControl->onButtonWasPressed(2);
remoteControl->offButtonWasPushed(2);
remoteControl->undoButtonWasPressed();
return 0;
}
c++子对象保存在父类对象数组里,应该保存指针,如果是直接保存的话,最后取出来变成父对象(自动转型?)onCommands = new Command*[7];类似语句出现的原因。