【HeadFirst 设计模式】命令模式的C++实现

一、案例背景

A公司想要生产一款万能遥控器:使用本公司遥控器,只需要设置好需要被控制的设备,比如电视,空调,电灯等,便可以实现遥控,从此摆脱找不到遥控器的情况!

其他电器类的代码和实现已经相应的电器公司提供,你的任务是将这些代码融合进遥控器中,以实现遥控器的功能。

二、案例分析

听起来很不错的一个主意,但是怎么实现呢?

看过了那么多的设计模式,我想"针对接口编程,而不是针对实现编程"这条设计原则已经给你留下了些许印象。你可以会说:我们可以设计一个抽象电器接口,让其他电器类继承自这个接口,然后在遥控器类中使用组合,应该就可以解决问题。是的,但是你忽略了一个问题,电器类的代码已经由各大厂商给出,我们并不能随意修改这些代码。

这种情形下,命令模式也许能解决你的问题。该模式可以使"发出命令"和"执行命令"解耦,发出命令的人只管发出命令而不必关注执行者是怎么完成命令的。想像以下你走进一家餐厅的时候,是不是也只需要看着菜单点菜(下发命令)就行了,而后厨的王牌厨师们也根本不需要关注是谁点了这道西红柿炒蛋,你们不也是已经实现解耦了吗?

三、代码案例

cpp 复制代码
#include <iostream>
#include <string>
#include <vector>
#include <array>

using namespace std;

class Command
{
public:
    virtual void execute() const = 0;
};

class Light
{
public:
    void on() const
    {
        cout << "灯开了..." << endl;
    }
    void off() const
    {
        cout << "灯关了..." << endl;
    }
};

class TV
{
public:
    void on() const
    {
        cout << "电视开了..." << endl;
    }
    void off() const
    {
        cout << "电视关了..." << endl;
    }
};

class AC
{
public:
    void on() const
    {
        cout << "空调开了..." << endl;
    }
    void off() const
    {
        cout << "空调关了..." << endl;
    }
};

class LightOnCommand : public Command
{
private:
    Light& light;

public:
    LightOnCommand(Light& light) : light(light) {}
    void execute() const override
    {
        light.on();
    }
};

class LightOffCommand : public Command
{
private:
    Light& light;

public:
    LightOffCommand(Light& light) : light(light) {}
    void execute() const override
    {
        light.off();
    }
};

class TVOnCommand : public Command
{
private:
    TV& tv;

public:
    TVOnCommand(TV& tv) : tv(tv) {}
    void execute() const override
    {
        tv.on();
    }
};

class TVOffCommand : public Command
{
private:
    TV& tv;

public:
    TVOffCommand(TV& tv) : tv(tv) {}
    void execute() const override
    {
        tv.off();
    }
};

class ACOnCommand : public Command
{
private:
    AC& ac;

public:
    ACOnCommand(AC& ac) : ac(ac) {}
    void execute() const override
    {
        ac.on();
    }
};

class ACOffCommand : public Command
{
private:
    AC& ac;

public:
    ACOffCommand(AC& ac) : ac(ac) {}
    void execute() const override
    {
        ac.off();
    }
};

class Remote
{
private:
    const static int              MAX_SLOT_SUM = 7;
    array<Command*, MAX_SLOT_SUM> onCommand {};
    array<Command*, MAX_SLOT_SUM> offCommand {};

    void execute(const int slot, array<Command*, MAX_SLOT_SUM>& command)
    {
        if (slot < MAX_SLOT_SUM)
        {
            if (command[slot])
            {
                command[slot]->execute();
            }
            else
            {
                std::cout << "slot " << slot << " is empty" << std::endl;
            }
        }
    }

public:
    Remote() {}

    void set(int slot, Command& on, Command& off)
    {
        if (slot < MAX_SLOT_SUM)
        {
            onCommand[slot]  = &on;
            offCommand[slot] = &off;
        }
    }

    void pressOnButton(const int slot)
    {
        execute(slot, onCommand);
    }

    void pressOffButton(const int slot)
    {
        execute(slot, offCommand);
    }
};

enum
{
    _TV,
    _Light,
    _AC
};

int main()
{
    Remote remote;

    TV           tv {};
    TVOnCommand  tvOnCommand(tv);
    TVOffCommand tvOffCommand(tv);
    remote.set(_TV, tvOnCommand, tvOffCommand);

    Light           light {};
    LightOnCommand  lightOnCommand(light);
    LightOffCommand lightOffCommand(light);
    remote.set(_Light, lightOnCommand, lightOffCommand);

    AC           ac {};
    ACOnCommand  acOnCommand(ac);
    ACOffCommand acOffCommand(ac);
    remote.set(_AC, acOnCommand, acOffCommand);

    remote.pressOnButton(0);
    remote.pressOnButton(1);
    remote.pressOnButton(2);
    cout << endl << endl;
    remote.pressOffButton(0);
    remote.pressOffButton(1);
    remote.pressOffButton(2);
    return 0;
}
相关推荐
mjhcsp11 小时前
C++ KMP 算法:原理、实现与应用全解析
java·c++·算法·kmp
好大哥呀11 小时前
C++ IDE
开发语言·c++·ide
WW_千谷山4_sch11 小时前
MYOJ_10599:CSP初赛题单10:计算机网络
c++·计算机网络·算法
梵尔纳多12 小时前
绘制一个矩形
c++·图形渲染·opengl
橘颂TA12 小时前
【剑斩OFFER】算法的暴力美学——leetCode 946 题:验证栈序列
c++·算法·leetcode·职场和发展·结构与算法
闻缺陷则喜何志丹12 小时前
【状态机动态规划】3686. 稳定子序列的数量|1969
c++·算法·动态规划·力扣·状态机动态规划
liulilittle12 小时前
OPENPPP2 网络驱动模式
开发语言·网络·c++·网络协议·信息与通信·通信
mjhcsp12 小时前
C++ AC 自动机:原理、实现与应用全解析
java·开发语言·c++·ac 自动机
茶本无香12 小时前
设计模式之二—原型模式:灵活的对象克隆机制
java·设计模式·原型模式
爱吃生蚝的于勒13 小时前
【Linux】进程间通信之匿名管道
linux·运维·服务器·c语言·数据结构·c++·vim