public function setCommand($command) { $this->slot = $command; } public function bottonWasPressd() { $this->slot->execute(); } } println(SEPARATE, '命令模式', SEPARATE); showImgs('6.png'); $control = new SimpleRemoteControl(); $light = new Light(); $command = new LightOnCommand($light); $control->setCommand($command); $control->bottonWasPressd(); /** * 输出: Light on */ class RemoteControl { public $onCommands = array(); public $offCommands = array(); public $lastCommand; #最后一个命令 public function __construct() { for ($i = 0; $i < 7; $i++) { $this->onCommands[$i] = new NoCommand(); $this->offCommands[$i] = new NoCommand();