bugfix: add destructors

master
Qiangguo Feng 2021-10-11 13:21:26 +08:00 committed by GitHub
parent 3614b77b9e
commit c103bb8897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 22 deletions

View File

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