简单遥控器

来源:互联网 发布:斐波那契数列c语言for 编辑:程序博客网 时间:2024/04/27 16:03
#include<cstdio>#include<iostream>using namespace std;class Command{public:    virtual void execute() {}};class Light{public:    void On()    {        cout<<"Light on"<<endl;    }    void Off()    {        cout<<"Light off"<<endl;    }};class LightOnCommand :public Command{private:    Light *light;public:    LightOnCommand(Light *light)    {        light=light;    }    void execute()    {        light->On();    }};class LightOffCommand :public Command{private:    Light *light;public:    LightOffCommand(Light *light)    {        light=light;    }    void execute()    {        light->Off();    }};class SimpleRemoteControl{public:    Command *slot;    SimpleRemoteControl() {}    void setCommand(Command *command)    {        slot=command;    }    void buttonWasPresseed()    {        slot->execute();    }};int main(){    SimpleRemoteControl *remote =new SimpleRemoteControl();    Light *light=new Light();    LightOnCommand *lightOn = new LightOnCommand(light);    remote->setCommand(lightOn);    remote->buttonWasPresseed();}

原创粉丝点击