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

186 lines
2.7 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;
2021-10-11 05:21:26 +00:00
// 命令队列类
2019-10-30 14:54:41 +00:00
#define COMMAND_QUEUE
2021-10-11 05:21:26 +00:00
// 抽象命令类 Command
2019-10-30 14:54:41 +00:00
class Command
{
public:
Command(){}
virtual ~Command(){}
2021-10-11 05:21:26 +00:00
// 声明抽象接口:发送命令
2019-10-30 14:54:41 +00:00
virtual void execute() = 0;
private:
Command *command;
};
2021-10-11 05:21:26 +00:00
// 接收者:电灯类
2019-10-30 14:54:41 +00:00
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;
};
2021-10-11 05:21:26 +00:00
// 接收者:风扇类
2019-10-30 14:54:41 +00:00
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;
};
2021-10-11 05:21:26 +00:00
// 具体命令类 LampCommand
2019-10-30 14:54:41 +00:00
class LampCommand :public Command
{
public:
LampCommand(){
2021-10-11 05:21:26 +00:00
printf("开关控制电灯\n");
2019-10-30 14:54:41 +00:00
lamp = new Lamp();
}
2021-10-29 13:33:26 +00:00
LampCommand(const LampCommand&) = delete;
LampCommand& operator=(const LampCommand&) = delete;
2021-10-11 05:21:26 +00:00
~LampCommand(){
delete lamp;
lamp = nullptr;
}
// 实现execute()
2019-10-30 14:54:41 +00:00
void execute(){
if (lamp->getLampState()){
lamp->off();
}
else{
lamp->on();
}
}
private:
Lamp *lamp;
};
2021-10-11 05:21:26 +00:00
// 具体命令类 FanCommand
2019-10-30 14:54:41 +00:00
class FanCommand :public Command
{
public:
FanCommand(){
2021-10-11 05:21:26 +00:00
printf("开关控制风扇\n");
2019-10-30 14:54:41 +00:00
fan = new Fan();
}
2021-10-29 13:33:26 +00:00
FanCommand(const FanCommand& a) = delete;
FanCommand& operator=(const FanCommand&) = delete;
2021-10-11 05:21:26 +00:00
~FanCommand(){
2021-10-29 13:33:26 +00:00
delete fan;
fan = nullptr;
2021-10-11 05:21:26 +00:00
}
// 实现execute()
2019-10-30 14:54:41 +00:00
void execute(){
if (fan->getFanState()){
fan->off();
}
else{
fan->on();
}
}
private:
Fan *fan;
};
2021-10-11 05:21:26 +00:00
// 调用者 Button
2019-10-30 14:54:41 +00:00
class Button
{
public:
Button(){}
2021-10-11 05:21:26 +00:00
// 注入具体命令类对象
2019-10-30 14:54:41 +00:00
void setCommand(Command *cmd){
this->command = cmd;
}
2021-10-11 05:21:26 +00:00
// 发送命令:触摸按钮
2019-10-30 14:54:41 +00:00
void touch(){
2021-10-11 05:21:26 +00:00
printf("触摸开关:");
2019-10-30 14:54:41 +00:00
command->execute();
}
private:
Command *command;
};
#ifdef COMMAND_QUEUE
/*************************************/
2021-10-11 05:21:26 +00:00
/* 命令队列 */
2019-10-30 14:54:41 +00:00
#include <vector>
2021-10-11 05:21:26 +00:00
// 命令队列类
2019-10-30 14:54:41 +00:00
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;
};
2021-10-11 05:21:26 +00:00
// 调用者
2019-10-30 14:54:41 +00:00
class Button2
{
public:
Button2(){}
2021-10-11 05:21:26 +00:00
// 注入具体命令队列类对象
2019-10-30 14:54:41 +00:00
void setCommandQueue(CommandQueue *cmdQueue){
this->cmdQueue = cmdQueue;
}
2021-10-11 05:21:26 +00:00
// 发送命令:触摸按钮
2019-10-30 14:54:41 +00:00
void touch(){
2021-10-11 05:21:26 +00:00
printf("触摸开关:");
2019-10-30 14:54:41 +00:00
cmdQueue->execute();
}
private:
CommandQueue *cmdQueue;
};
#endif
2021-10-11 05:21:26 +00:00
#endif //__COMMAND_PATTERN_H__