设计模式:命令模式(6)C++版

命令模式:将请求封装成对象。


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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值