DesignPattern/15.CommandPattern/2.Code/CommandPattern.h

173 lines
2.3 KiB
C
Raw Normal View History

2019-10-30 14:54:41 +00:00
#ifndef __COMMAND_PATTERN_H__
#define __COMMAND_PATTERN_H__
#include <mutex>
#include <time.h>
using namespace std;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define COMMAND_QUEUE
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Command
class Command
{
public:
Command(){}
virtual ~Command(){}
2019-10-30 14:54:41 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿڣ<D3BF><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
virtual void execute() = 0;
private:
Command *command;
};
// <20><><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class Lamp
{
public :
Lamp(){
this->lampState = false;
}
void on(){
lampState = true;
printf("Lamp is on\n");
}
void off(){
lampState = false;
printf("Lamp is off\n");
}
bool getLampState(){
return lampState;
}
private:
bool lampState;
};
// <20><><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class Fan
{
public:
Fan(){
this->fanState = false;
}
void on(){
fanState = true;
printf("Fan is on\n");
}
void off(){
fanState = false;
printf("Fan is off\n");
}
bool getFanState(){
return fanState;
}
private:
bool fanState;
};
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> LampCommand
class LampCommand :public Command
{
public:
LampCommand(){
printf("<EFBFBD><EFBFBD><EFBFBD>ؿ<EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD>\n");
lamp = new Lamp();
}
// ʵ<><CAB5>execute()
void execute(){
if (lamp->getLampState()){
lamp->off();
}
else{
lamp->on();
}
}
private:
Lamp *lamp;
};
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> FanCommand
class FanCommand :public Command
{
public:
FanCommand(){
printf("<EFBFBD><EFBFBD><EFBFBD>ؿ<EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD><EFBFBD>\n");
fan = new Fan();
}
// ʵ<><CAB5>execute()
void execute(){
if (fan->getFanState()){
fan->off();
}
else{
fan->on();
}
}
private:
Fan *fan;
};
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Button
class Button
{
public:
Button(){}
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void setCommand(Command *cmd){
this->command = cmd;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
void touch(){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:");
command->execute();
}
private:
Command *command;
};
#ifdef COMMAND_QUEUE
/*************************************/
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#include <vector>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class CommandQueue
{
public:
CommandQueue(){
}
void addCommand(Command *cmd){
commandQueue.push_back(cmd);
}
void execute(){
for (int i = 0; i < commandQueue.size(); i++)
{
commandQueue[i]->execute();
}
}
private:
vector<Command*>commandQueue;
};
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class Button2
{
public:
Button2(){}
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void setCommandQueue(CommandQueue *cmdQueue){
this->cmdQueue = cmdQueue;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
void touch(){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:");
cmdQueue->execute();
}
private:
CommandQueue *cmdQueue;
};
#endif
#endif //__COMMAND_PATTERN_H__