bugfix: add destructors
parent
3614b77b9e
commit
c103bb8897
|
@ -5,22 +5,22 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// 命令队列类
|
// 命令队列类
|
||||||
#define COMMAND_QUEUE
|
#define COMMAND_QUEUE
|
||||||
|
|
||||||
// 抽象命令类 Command
|
// 抽象命令类 Command
|
||||||
class Command
|
class Command
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Command(){}
|
Command(){}
|
||||||
virtual ~Command(){}
|
virtual ~Command(){}
|
||||||
// 声明抽象接口:发送命令
|
// 声明抽象接口:发送命令
|
||||||
virtual void execute() = 0;
|
virtual void execute() = 0;
|
||||||
private:
|
private:
|
||||||
Command *command;
|
Command *command;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 接收者:电灯类
|
// 接收者:电灯类
|
||||||
class Lamp
|
class Lamp
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
|
@ -42,7 +42,7 @@ private:
|
||||||
bool lampState;
|
bool lampState;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 接收者:风扇类
|
// 接收者:风扇类
|
||||||
class Fan
|
class Fan
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -64,15 +64,19 @@ private:
|
||||||
bool fanState;
|
bool fanState;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体命令类 LampCommand
|
// 具体命令类 LampCommand
|
||||||
class LampCommand :public Command
|
class LampCommand :public Command
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LampCommand(){
|
LampCommand(){
|
||||||
printf("开关控制电灯\n");
|
printf("开关控制电灯\n");
|
||||||
lamp = new Lamp();
|
lamp = new Lamp();
|
||||||
}
|
}
|
||||||
// 实现execute()
|
~LampCommand(){
|
||||||
|
delete lamp;
|
||||||
|
lamp = nullptr;
|
||||||
|
}
|
||||||
|
// 实现execute()
|
||||||
void execute(){
|
void execute(){
|
||||||
if (lamp->getLampState()){
|
if (lamp->getLampState()){
|
||||||
lamp->off();
|
lamp->off();
|
||||||
|
@ -85,15 +89,19 @@ private:
|
||||||
Lamp *lamp;
|
Lamp *lamp;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 具体命令类 FanCommand
|
// 具体命令类 FanCommand
|
||||||
class FanCommand :public Command
|
class FanCommand :public Command
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FanCommand(){
|
FanCommand(){
|
||||||
printf("开关控制风扇\n");
|
printf("开关控制风扇\n");
|
||||||
fan = new Fan();
|
fan = new Fan();
|
||||||
}
|
}
|
||||||
// 实现execute()
|
~FanCommand(){
|
||||||
|
delete lamp;
|
||||||
|
lamp = nullptr;
|
||||||
|
}
|
||||||
|
// 实现execute()
|
||||||
void execute(){
|
void execute(){
|
||||||
if (fan->getFanState()){
|
if (fan->getFanState()){
|
||||||
fan->off();
|
fan->off();
|
||||||
|
@ -106,18 +114,18 @@ private:
|
||||||
Fan *fan;
|
Fan *fan;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 调用者 Button
|
// 调用者 Button
|
||||||
class Button
|
class Button
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Button(){}
|
Button(){}
|
||||||
// 注入具体命令类对象
|
// 注入具体命令类对象
|
||||||
void setCommand(Command *cmd){
|
void setCommand(Command *cmd){
|
||||||
this->command = cmd;
|
this->command = cmd;
|
||||||
}
|
}
|
||||||
// 发送命令:触摸按钮
|
// 发送命令:触摸按钮
|
||||||
void touch(){
|
void touch(){
|
||||||
printf("触摸开关:");
|
printf("触摸开关:");
|
||||||
command->execute();
|
command->execute();
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
|
@ -126,10 +134,10 @@ private:
|
||||||
|
|
||||||
#ifdef COMMAND_QUEUE
|
#ifdef COMMAND_QUEUE
|
||||||
/*************************************/
|
/*************************************/
|
||||||
/* 命令队列 */
|
/* 命令队列 */
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// 命令队列类
|
// 命令队列类
|
||||||
class CommandQueue
|
class CommandQueue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -149,18 +157,18 @@ private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 调用者
|
// 调用者
|
||||||
class Button2
|
class Button2
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Button2(){}
|
Button2(){}
|
||||||
// 注入具体命令队列类对象
|
// 注入具体命令队列类对象
|
||||||
void setCommandQueue(CommandQueue *cmdQueue){
|
void setCommandQueue(CommandQueue *cmdQueue){
|
||||||
this->cmdQueue = cmdQueue;
|
this->cmdQueue = cmdQueue;
|
||||||
}
|
}
|
||||||
// 发送命令:触摸按钮
|
// 发送命令:触摸按钮
|
||||||
void touch(){
|
void touch(){
|
||||||
printf("触摸开关:");
|
printf("触摸开关:");
|
||||||
cmdQueue->execute();
|
cmdQueue->execute();
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue