命令模式

最经典的例子是餐馆点餐菜。服务员用菜单记录,厨师按照菜单做菜。服务员不需要知道菜具体怎么做的。还可以实现日志记录,最后照着菜单付钱。

#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];类似语句出现的原因。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值