一天一个设计模式(13)——命令模式
Posted Bin_x
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一天一个设计模式(13)——命令模式相关的知识,希望对你有一定的参考价值。
<?php class Bulb { public function turnOn() { echo ‘Bulb has turned on‘; } public function turnOff() { echo ‘Bulb has turned off‘; } } interface Order { public function execute(); public function undo(); public function redo(); } class TurnOn implements Order { private $_bulb; public function __construct(Bulb $bulb) { $this->_bulb = $bulb; } public function execute() { $this->_bulb->turnOn(); } public function undo() { $this->_bulb->turnOff(); } public function redo() { $this->_bulb->turnOn(); } } class TurnOff implements Order { private $_bulb; public function __construct($bulb) { $this->_bulb = $bulb; } public function execute() { $this->_bulb->turnOff(); } public function undo() { $this->_bulb->turnOn(); } public function redo() { $this->_bulb->turnOff(); } } class Control { public function submit($order) { $order->execute(); } } $bulb = new Bulb(); $turnOn = new TurnOn($bulb); $turnOff = new TurnOff($bulb); $control = new Control(); $control->submit($turnOn); echo PHP_EOL; $control->submit($turnOff);
以上是关于一天一个设计模式(13)——命令模式的主要内容,如果未能解决你的问题,请参考以下文章
设计模式(13)----- 命令设计模式(升级----加一个撤销的命令)