设计模式入门,命令模式,c++代码实现

// 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;
}


posted @ 2017-07-03 14:39  wangting235  阅读(128)  评论(0编辑  收藏  举报