設計模式 命令模式

 命令模式

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.
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章