代码如下:
#include <iostream>
#include <queue>
#include <Windows.h>
using namespace std;class HandleClientProtocol
{
public:void addMoney(){cout << "给玩家增加金币" << endl;}void addDiamond(){cout << "给玩家增加钻石" << endl;}void addEquipment(){cout << "给玩家穿装备" << endl;}void addLevel(){cout << "给玩家升级" << endl;}
};class AbstractCommand
{
public:virtual void handle() = 0;
};class AddMoneyCommand :public AbstractCommand
{
public:AddMoneyCommand(HandleClientProtocol* protocol) :protocol(protocol) {}virtual void handle(){this->protocol->addMoney();}HandleClientProtocol *protocol;
};class AddDiamondCommand :public AbstractCommand
{
public:AddDiamondCommand(HandleClientProtocol* protocol) :protocol(protocol) {}virtual void handle(){this->protocol->addDiamond();}HandleClientProtocol *protocol;
};class AddEquipmentCommand :public AbstractCommand
{
public:AddEquipmentCommand(HandleClientProtocol* protocol) :protocol(protocol) {}virtual void handle(){this->protocol->addEquipment();}HandleClientProtocol *protocol;
};class AddLevelCommand :public AbstractCommand
{
public:AddLevelCommand(HandleClientProtocol* protocol) :protocol(protocol) {}virtual void handle(){this->protocol->addLevel();}HandleClientProtocol *protocol;
};class Serser {public:void addRequest(AbstractCommand *command){commands.push(command);}void startHandle(){while (!commands.empty()){Sleep(2000);AbstractCommand *command = commands.front();command->handle();commands.pop();}}queue<AbstractCommand *> commands;};void test01()
{HandleClientProtocol *protocol = new HandleClientProtocol;AbstractCommand *addmoney = new AddMoneyCommand(protocol);AbstractCommand *adddiamond = new AddDiamondCommand(protocol);AbstractCommand *addequipment = new AddEquipmentCommand(protocol);AbstractCommand *addlevel = new AddLevelCommand(protocol);Serser *server = new Serser;server->addRequest(addmoney);server->addRequest(adddiamond);server->addRequest(addequipment);server->addRequest(addlevel);server->startHandle();}int main()
{test01();return 0;
}
测试结果: