// 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. jquery源码解析:expando,holdReady,ready详解

    jQuery的工具方法,其实就是静态方法,源码里面就是通过extend方法,把这些工具方法添加给jQuery构造函数的. jQuery.extend({       //当只有一个对象时,就把这个对象 ...

  2. [转] Linux 安装.src.rpm源码包的方法

    方法一:以setarch-1.3-1.src.rpm 软件包为例(可以到CSDN http://download.csdn.net/source/215173#acomment下载) 假设该文件已经存 ...

  3. H5新手教程,小白来看看。

    H5教程(一) 相信点进来看这篇文章的应该都是刚刚接触H5的新手,那么你真的是找到了一篇合适的文章. 1.学习前准备 既然想学习好H5,只是这样看是不够的,还需要动手练习,以及及时复习,所以我推荐几款 ...

  4. P1158 导弹拦截

    P1158 导弹拦截 思路: 按每个点到第一个系统的距离排序,然后预处理出每个点及其之后的点到第二个系统的距离的最大值,再循环一遍枚举答案.  代码: #include <cstdio> ...

  5. Android 开发者文档 -- 应用基础知识

    https://developer.android.com/guide/components/fundamentals 应用基础知识 Android 应用采用 Java 编程语言编写.Android ...

  6. 实验三敏捷开发与XP实践《Java开发环境的熟悉》实验报告

    一.实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报 ...

  7. [Alpha]Scrum Meeting#6

    github 本次会议项目由PM召开,时间为4月8日晚上10点30分 时长25分钟 任务表格 人员 昨日工作 下一步工作 木鬼 整理开会记录 撰写并发布之前因为清明耽误的博客 SiMrua 寻找方法捕 ...

  8. bzoj3280: 小R的烦恼(最小费用最大流)

    Description 小R最近遇上了大麻烦,他的程序设计挂科了.于是他只好找程设老师求情.善良的程设老师答应不挂他,但是要 求小R帮助他一起解决一个难题.问题是这样的,程设老师最近要进行一项邪恶的实 ...

  9. 关于锚点定位,ul设置fixed后,div被覆盖一部分的问题

    例如: 问题: 刚开始的时候 .ul是正常显示的,当页面的滚动条滚动到一定的高度是 ,ul就被设置为 position:fixed:那么 点击 li相对应div就会被 固定定位的ul覆盖住一部分. 解 ...

  10. OnClick和OnClientClick

    OnClientClick是客户端事件处理方法,一般采用JavaScript来进行处理,也就是直接在IE端运行,一点击就运行 OnClick是服务器端事件处理方法,在服务器端也就是IIS中运行,点击后 ...