// 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. 洛谷P3806 【模板】点分治1

    题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接下来m行每行询问一个K 输出格式: 对于 ...

  2. ACM-ICPC 2018北京网络赛-A题 Saving Tang Monk II-优先队列

    做法:优先队列模板题,按步数从小到大为优先级,PASS掉曾经以相同氧气瓶走过的地方就好了 题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制: ...

  3. 浅析通过"监控"来辅助进行漏洞挖掘

    这篇文章总结了一些笔者个人在漏洞挖掘这一块的"姿势",看了下好像也没相关类似TIPs或者文章出现,就写下此文. 本文作者:Auther : vulkey@MstLab(米斯特安全攻 ...

  4. [Objective-C语言教程]基本语法(4)

    前面已经看到了Objective-C程序的基本结构,因此很容易理解Objective-C编程语言的其他基本构建块. Objective-C令牌 Objective-C程序由各种令牌组成,令牌可以是关键 ...

  5. ArchLinux 下 virtualbox 报错 libQtCore.so.4: cannot open shared object file

    VirtualBox: supR3HardenedMainGetTrustedMain: dlopen("/usr/lib/virtualbox/VirtualBox.so",) ...

  6. python爬虫学习心得

    作为一名python的忠实爱好者,我开始接触爬虫是在2017年4月份,最开始接触它的时候遇到两个梗,一个是对python还不算太了解(当然现在也仍然在努力学习它的有关内容),二是对爬虫心怀一份敬畏之心 ...

  7. vue 数据(data)赋值问题

    总结一下我遇到的一个纠结很久的问题. 在项目中需要用到后台的数据对前端渲染,使用到了vue整合的axios,使用vue中的钩子函数在页面组件挂载完成之后向后台发送一个get请求然后将返回后的数据赋值d ...

  8. Hystrix - 踩坑回忆

    1.Unable to connect to Command Metric Stream 异常 Finchley版本使用Hystrix存在此问题.网上常规解决思路: @Bean public Serv ...

  9. 牛客练习赛28-B(线段树,区间更新)

    牛客练习赛28 - B 传送门 题目 qn姐姐最好了~ ​ qn姐姐给你了一个长度为n的序列还有m次操作让你玩, ​ 1 l r 询问区间[l,r]内的元素和 ​ 2 l r 询问区间[l,r]内的 ...

  10. Hive优化-大表join大表优化

    Hive优化-大表join大表优化 5.大表join大表优化 如果Hive优化实战2中mapjoin中小表dim_seller很大呢?比如超过了1GB大小?这种就是大表join大表的问题.首先引入一个 ...