设计模式 命令模式

 命令模式

interface Command
{
    public function execute();
}

class Light
{
    public function on()
    {
        echo "on";
    }
}

class SimpleRemoteControl
{
    private $slot = null;

    public function __construct()
    {
    }

    public  function setCommand($command) {
        $this->slot = $command;
    }

    public function buttonWasPressed()
    {
        $this->slot->execute();
    }

}

class LightOnCommand implements Command
{
    private $light = null;

    public function __construct($light)
    {
        $this->light  = $light;
    }

    public function execute()
    {
        $this->light->on();
        // TODO: Implement execute() method.
    }
}
发布了67 篇原创文章 · 获赞 6 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/sfmcatl/article/details/83722810