1. 命令接收者
//灯
class Lamp
{
public:
Lamp()
{
m_bLampState = false;
std::cout << "Lamp 构造函数" << std::endl;
}
~Lamp()
{
std::cout << "Lamp 析构函数" << std::endl;
}
void switchLampOn()
{
m_bLampState = true;
std::cout << "Lamp 打开状态" << std::endl;
}
void switchLampOff()
{
m_bLampState = false;
std::cout << "Lamp 关闭状态" << std::endl;
}
bool getLampState()
{
return m_bLampState;
}
private:
bool m_bLampState;
};
//风扇
class Fan
{
public:
Fan()
{
m_bFanState = false;
std::cout << "Fan 构造函数" << std::endl;
}
~Fan()
{
std::cout << "Fan 析构函数" << std::endl;
}
void switchFanOn()
{
m_bFanState = true;
std::cout << "Fan 打开状态" << std::endl;
}
void switchFanOff()
{
m_bFanState = false;
std::cout << "Fan 关闭状态" << std::endl;
}
bool getFanState()
{
return m_bFanState;
}
private:
bool m_bFanState;
};
2. Command(抽象命令行)
class Command
{
public:
virtual ~Command() = default;
//生命抽象接口:执行命令
virtual void execute() = 0;
protected:
Command() = default;
};
3. ConcreteCommand(具体命令行)
//开关灯命令
class LampCommand : public Command
{
public:
LampCommand()
{
std::cout << "LampCommand 构造函数" << std::endl;
m_pLamp = new Lamp();
}
~LampCommand()
{
std::cout << "LampCommand 析构函数" << std::endl;
delete m_pLamp;
}
void execute()
{
if (m_pLamp->getLampState())
{
m_pLamp->switchLampOff();
}
else {
m_pLamp->switchLampOn();
}
}
private:
Lamp *m_pLamp;
};
//开关风扇命令
class FanCommand : public Command
{
public:
FanCommand()
{
std::cout << "FanCommand 构造函数" << std::endl;
m_pFan = new Fan();
}
~FanCommand()
{
std::cout << "FanCommand 析构函数" << std::endl;
delete m_pFan;
}
void execute()
{
if (m_pFan->getFanState())
{
m_pFan->switchFanOff();
}
else {
m_pFan->switchFanOn();
}
}
private:
Fan *m_pFan;
};
4. CommandVector(命令集合)
class CommandVector
{
public:
CommandVector()
{
std::cout << "CommandVector 构造函数" << std::endl;
}
~CommandVector()
{
std::cout << "CommandVector 析构函数" << std::endl;
for (auto cmd: m_CommandVector)
{
delete cmd;
}
}
void addCommand(Command* cmd)
{
m_CommandVector.push_back(cmd);
}
void execute()
{
for (auto cmd : m_CommandVector)
{
cmd->execute();
}
}
private:
std::vector<Command*> m_CommandVector;
};
5. Invoker(调用者)
5.1. 单命令
class Switch
{
public:
Switch()
{
std::cout << "Switch 构造函数" << std::endl;
m_pCommand = nullptr;
}
~Switch()
{
std::cout << "Switch 析构函数" << std::endl;
if (m_pCommand)
{
delete m_pCommand;
}
}
//传入具体命令类对象指针
void setCommand(Command* cmd)
{
if (m_pCommand)
{
delete m_pCommand;//删除旧的命令
}
m_pCommand = cmd;
}
//发送命令:切换开关
void touch()
{
std::cout << "切换开关:" << std::endl;
if (nullptr != m_pCommand)
{
m_pCommand->execute();
}
}
private:
Command* m_pCommand;
};
5.2. 主函数
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// 实例化调用者:开关 (调用者)
auto *pSwitch = new Switch();
// (具体命令),Switch中函数setCommand和析构会处理命令Command
Command *lampCmd, *fanCmd;
std::cout << "===按钮控制电灯====" << std::endl;
lampCmd = new LampCommand(); // 具体命令
pSwitch->setCommand(lampCmd);
pSwitch->touch(); // 开
pSwitch->touch(); // 关
pSwitch->touch(); // 开
std::cout << "====按钮控制风扇====" << std::endl;
fanCmd = new FanCommand();
pSwitch->setCommand(fanCmd);
pSwitch->touch(); // 开
pSwitch->touch(); // 关
pSwitch->touch(); // 开
std::cout << "==============" << std::endl;
delete pSwitch;
getchar();
return 0;
}
6. Invoker(调用者)
6.1. 命令集合
class SwitchVector
{
public:
SwitchVector()
{
std::cout << "SwitchVector 析构函数" << std::endl;
m_pCommandVector = nullptr;
}
~SwitchVector()
{
std::cout << "SwitchVector 析构函数" << std::endl;
if (m_pCommandVector)
{
delete m_pCommandVector;
}
}
//传入具体命令类对象指针
void setCommandVector(CommandVector* cmdVector)
{
if (m_pCommandVector)
{
delete m_pCommandVector;//删除旧的命令
}
m_pCommandVector = cmdVector;
}
//发送命令:切换开关
void touch()
{
std::cout << "切换开关:" << std::endl;
if (nullptr != m_pCommandVector)
{
m_pCommandVector->execute();
}
}
private:
CommandVector* m_pCommandVector;
};
6.2. 主函数
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// 实例化调用者:开关 (调用者)
auto *pSwitchVector = new SwitchVector();
//(具体命令),pSwitchVector中析构会处理commandVector
Command *lampCmd, *fanCmd;
// 命令集合
auto *commandVector = new CommandVector();
//按钮控制电灯
lampCmd = new LampCommand(); // 具体命令
commandVector->addCommand(lampCmd);
// 按钮控制风扇
fanCmd = new FanCommand();
commandVector->addCommand(fanCmd);
pSwitchVector->setCommandVector(commandVector);
pSwitchVector->touch(); // 开
pSwitchVector->touch(); // 关
pSwitchVector->touch(); // 开
delete pSwitchVector;
getchar();
return 0;
}