// test06.cpp : Defines the entry point for the console application.
//
//设计模式第5章 命令模式
#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;
class Command
{
public:
    virtual void execute(){}
};

class Light
{
    string position;
public:
    Light(string strPos)
    {
        position = strPos;
    }
public:
    void on()
    {
        cout<<position<<"Light is on"<<endl;
    }

void off()
    {
        cout<<position<<"Light is off"<<endl;
    }
};

class CeilingFan
{
    string position;
public:
    CeilingFan(string strPos)
    {
        position = strPos;
    }
public:
    void high()
    {
        cout<<position<<"ceiling fan is on high"<<endl;
    }

void medium()
    {
        cout<<position<<"ceiling fan is on medium"<<endl;
    }
    void off()
    {
        cout<<position<<"ceiling fan is off"<<endl;
    }
};

class GarageDoor
{
    string position;
public:
    GarageDoor(string strPos)
    {
        position = strPos;
    }
public:
    void up()
    {
        cout<<position<<"garage door is up"<<endl;
    }

void down()
    {
        cout<<position<<"garage door is down"<<endl;
    }
};

class Stereo
{
    string position;
public:
    Stereo(string strPos)
    {
        position = strPos;
    }

public:
    void on()
    {
        cout<<position<<"stereo is on"<<endl;
    }

void off()
    {
        cout<<position<<"stereo is off"<<endl;
    }

void setCD()
    {
        cout<<position<<"stereo is set for CD"<<endl;
    }

void setVolume(int volume)
    {
        cout<<position<<"stereo volume set to "<<volume<<endl;
    }
};

class LightOnCommand : public Command
{
    Light* light;
public:
    LightOnCommand(Light* light)
    {
        this->light = light;
    }
    void execute()
    {
        light->on();
    }
};

class LightOffCommand:public Command
{
    Light* light;
public:
    LightOffCommand(Light* light)
    {
        this->light = light;
    }

void execute()
    {
        light->off();
    }
};

class CeilingFanOnCommand:public Command
{
    CeilingFan* ceilingfan;
public:
    CeilingFanOnCommand(CeilingFan* pCeilingFan)
    {
        ceilingfan = pCeilingFan;
    }

void execute()
    {
        ceilingfan->high();
    }
};

class CeilingFanOffCommand:public Command
{
    CeilingFan* ceilingfan;
public:
    CeilingFanOffCommand(CeilingFan* pCeilingFan)
    {
        ceilingfan = pCeilingFan;
    }

void execute()
    {
        ceilingfan->off();
    }
};

class GarageDoorUpCommand:public Command
{
    GarageDoor* garagedoor;
public:
    GarageDoorUpCommand(GarageDoor* pGaragedoor)
    {
        garagedoor = pGaragedoor;
    }

void execute()
    {
        garagedoor->up();
    }
};

class GarageDoorDownCommand:public Command
{
    GarageDoor* garagedoor;
public:
    GarageDoorDownCommand(GarageDoor* pGaragedoor)
    {
        garagedoor = pGaragedoor;
    }

void execute()
    {
        garagedoor->down();
    }
};

class StereoOnWithCDCommand:public Command
{
    Stereo* stereo;

public:
    StereoOnWithCDCommand(Stereo* pStereo)
    {
        stereo = pStereo;
    }

void execute()
    {
        stereo->on();
        stereo->setCD();
        stereo->setVolume(11);
    }
};

class StereoOffCommand:public Command
{
    Stereo* stereo;

public:
    StereoOffCommand(Stereo* pStereo)
    {
        stereo = pStereo;
    }

void execute()
    {
        stereo->off();
    }
};

class NoCommand:public Command
{
public:
    void execute(){}
};

class RemoteControl
{
    Command** onCommands;
    Command** offCommands;

public:
    RemoteControl()
    {
        onCommands = new Command* [7];
        offCommands = new Command* [7];

Command* noCommand = new NoCommand();
        for(int i = 0; i< 7; i++)
        {
            onCommands[i] = noCommand;
            offCommands[i] = noCommand;
        }
    }

void setCommand(int slot, Command* onCommand, Command* offCommand)
    {
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }

void onButtonWasPushed(int slot)
    {
        onCommands[slot]->execute();
    }

void offButtonWasPushed(int slot)
    {
        offCommands[slot]->execute();
    }

};
int _tmain(int argc, _TCHAR* argv[])
{
    RemoteControl remoteControl;

Light livingroomLight("Living Room");
    Light kitchenLight("Kitchen");
    CeilingFan ceilingfan("Livint Room");
    GarageDoor garadoor("");
    Stereo stereo("Livint Room");

LightOnCommand* livingroomLightOn = new LightOnCommand(&livingroomLight);
    LightOffCommand* livintroomLightOff = new LightOffCommand(&livingroomLight);
    LightOnCommand* kitchenLightOn = new LightOnCommand(&kitchenLight);
    LightOffCommand* kitchenLightOff = new LightOffCommand(&kitchenLight);

CeilingFanOnCommand* ceilingFanOn = new CeilingFanOnCommand(&ceilingfan);
    CeilingFanOffCommand* ceilingFanOff = new CeilingFanOffCommand(&ceilingfan);

GarageDoorUpCommand* garageDoorUp = new GarageDoorUpCommand(&garadoor);
    GarageDoorDownCommand* garageDoorDown = new GarageDoorDownCommand(&garadoor);

StereoOnWithCDCommand* stereoOnWithCD = new StereoOnWithCDCommand(&stereo);
    StereoOffCommand* stereoOff = new StereoOffCommand(&stereo);

remoteControl.setCommand(0,livingroomLightOn,livintroomLightOff);
    remoteControl.setCommand(1,kitchenLightOn,kitchenLightOff);
    remoteControl.setCommand(2,ceilingFanOn,ceilingFanOff);
    remoteControl.setCommand(3,stereoOnWithCD,stereoOff);

remoteControl.onButtonWasPushed(0);
    remoteControl.offButtonWasPushed(0);
    remoteControl.onButtonWasPushed(1);
    remoteControl.offButtonWasPushed(1);
    remoteControl.onButtonWasPushed(2);
    remoteControl.offButtonWasPushed(2);
    remoteControl.onButtonWasPushed(3);
    remoteControl.offButtonWasPushed(3);
    return 0;
}

设计模式入门,命令模式,c++代码实现的更多相关文章

  1. 设计模式入门,策略模式,c++代码实现

    // test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...

  2. 设计模式入门,单件模式,c++代码实现

    // test05.cpp : Defines the entry point for the console application.// #include "stdafx.h" ...

  3. 设计模式入门,工厂模式,c++代码实现

    // test04.cpp : Defines the entry point for the console application.////设计模式第4章 工厂模式#include "s ...

  4. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  5. 面向对象设计模式_命令模式(Command)解读

    在.Net框架中很多对象的方法中都会有Invoke方法,这种方法的设计实际是用了设计模式的命令模式, 模式图如下 其核心思路是将Client 向Receiver发送的命令行为进行抽象(ICommand ...

  6. 折腾Java设计模式之命令模式

    博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...

  7. 用Java 8 Lambda表达式实现设计模式:命令模式

    在这篇博客里,我将说明如何在使用 Java 8 Lambda表达式 的函数式编程方式 时实现 命令 设计模式 .命令模式的目标是将请求封装成一个对象,从对客户端的不同类型请求,例如队列或日志请求参数化 ...

  8. 设计模式之命令模式-JS

    理解命令模式 假设有一个快餐店,而我是该餐厅的点餐服务员,那么我一天的工作应该是这样的:当某位客人点餐或者打来订餐电话后,我会把他的需求都写在清单上,然后交给厨房,客人不用关心是哪些厨师帮他炒菜.我们 ...

  9. python设计模式之命令模式

    python设计模式之命令模式 现在多数应用都有撤销操作.虽然难以想象,但在很多年里,任何软件中确实都不存在撤销操作.撤销操作是在1974年引入的,但Fortran和Lisp分别早在1957年和195 ...

随机推荐

  1. [AIR] 使用操作系统默认应用程序打开文件

    AIR 2.0及以上提供了非常简单易用的API让你使用操作系统所定义的关联应用程序打开文件. 这使得使用AIR开发基于“文件管理器”的应用称为可能 用法如下: var file:File = File ...

  2. Your branch is ahead of 'origin/master' by 1 commit.

    git reset HEAD^ --soft git reset HEAD^ --hard --soft 表示保留当前commit,重新commit --hard 表示丢弃当前add,重新add.co ...

  3. [Python]json 错误xx is not JSON serializable

    TypeError: Decimal('1457501') is not JSON serializable 在使用json的时候经常会遇到xxx  is not JSON serializable, ...

  4. h5聊天工具的开发过程及思路

    这个产品的主要技术栈有,网易nim即时通信,vue-cli,muse-ui 1.在拿到这个需求时,脑袋里空的,什么想法都没有,完全懵逼,进了网易云通信的官网api查看,由于我做的是客户端的,所以重点看 ...

  5. 条目二十四《当效率至关重要时,请在map::operator[]与map::insert之间谨慎做出选择》

    条目二十四<当效率至关重要时,请在map::operator[]与map::insert之间谨慎做出选择> 当效率至关重要时,应该在map::operator[]和map::insert之 ...

  6. 干掉Vivado幺蛾子(2)-- 快速替换debug probes

    目录 1. 什么是ECO 2. 操作步骤 参考文献: 我们做项目,进入找bug阶段时,需要用ILA捕获相关的信号.之前我做项目,每改动一次探针(debug probes),都要重新综合.实现,通常要花 ...

  7. c# Config配置文件读写

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  8. #PHP 数组添加元素、统计数组相同元素个数、改变数组key值~_~

    一.数组添加元素 1.定义和用法: array_push() 函数向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度. 2.语法: array_push(array,value1, ...

  9. mysql for mac 上的安装及用DataGrip连接

    ---恢复内容开始--- 1.首先下载MySQL的mac版本,地址百度就行了. 2.这个时候需要注意安装的时候,弹出来的一个类似窗口,上面有提示默认密码,但是我当时就忘记了这个默认密码,如果你记住了默 ...

  10. CDH集群安装配置(四)- mysql 的安装

    安装mysql,并且创建相关的表(只需要在chd1上面安装而且需要root权限)1.1 查看Centos自带mysql是否已经安装 yum list installed | grep mysql 卸载 ...