聲明:本系列部落格參考資料《大話設計模式》,作者程傑。
命令模式:在軟體系統中,“行為要求者”與“行為實現者”通常呈現一種“緊耦合”。但在某些場合,比如要對行為進行“記錄、撤銷/重做、事務”等處理,這種無法抵禦變化的緊耦合是不合適的。在這種情況下,如何將“行為要求者”與“行為實現者”解耦?將一組行為抽象為對象,實現二者之間的松耦合。這就是命令模式。
類圖:
角色分析:
抽象命令:定義命令的介面,聲明執行的方法。
具體命令:命令介面實現對象,是“虛”的實現;通常會持有接收者,並調用接收者的功能來完成命令要執行的操作。
命令接收者:接收者,真正執行命令的對象。任何類都可能成為一個接收者,只要它能夠實現命令要求實現的相應功能。
控制者:要求命令對象執行請求,通常會持有命令對象,可以持有很多的命令對象。這個是用戶端真正觸發命令並要求命令執行相應操作的地方,也就是說相當於使用命令對象的入口。
核心代碼:
"; } /** * 關閉電視機 */ public function turnOff() { echo "The television is off."."
"; } /**切換頻道 * @param $channel 頻道 */ public function turnChannel($channel) { $this->curr_channel=$channel; echo "This TV Channel is ".$this->curr_channel."
"; }}/**執行命令介面 * Interface ICommand */interface ICommand{ function execute();}/**開機命令 * Class CommandOn */class CommandOn implements ICommand{ private $tv; public function __construct($tv) { $this->tv=$tv; } public function execute() { $this->tv->turnOn(); }}/**關機命令 * Class CommandOn */class CommandOff implements ICommand{ private $tv; public function __construct($tv) { $this->tv=$tv; } public function execute() { $this->tv->turnOff(); }}/**切換頻道命令 * Class CommandOn */class CommandChannel implements ICommand{ private $tv; private $channel; public function __construct($tv,$channel) { $this->tv=$tv; $this->channel=$channel; } public function execute() { $this->tv->turnChannel($this->channel); }}/**遙控器 * Class Control */class Control{ private $_onCommand; private $_offCommand; private $_changeChannel; public function __construct($on,$off,$channel) { $this->_onCommand = $on; $this->_offCommand = $off; $this->_changeChannel = $channel; } public function turnOn() { $this->_onCommand->execute(); } public function turnOff() { $this->_offCommand->execute(); } public function changeChannel() { $this->_changeChannel->execute(); }}
調用用戶端代碼:
header("Content-Type:text/html;charset=utf-8");//----------------------命令模式--------------------require_once "./Command/Command.php";//測試代碼// 命令接收者 $myTv = new Tv();// 開機命令 $on = new CommandOn($myTv);// 關機命令 $off = new CommandOff($myTv);// 頻道切換命令 $channel = new CommandChannel($myTv, 2);// 命令控制對象 $control = new Control($on, $off, $channel);// 開機 $control->turnOn();// 切換頻道 $control->changeChannel();// 關機 $control->turnOff();
適用情境:
1.系統需要將請求調用者和請求接收者解耦,使得調用者和接收者不直接互動。
2.系統需要在不同的時間指定請求、將請求排隊和執行請求。
3.系統需要支援命令的撤銷(Undo)操作和恢複(Redo)操作。
4.系統需要將一組操作組合在一起,即支援宏命令。
優點
1.降低對象之間的耦合度。
2.新的命令可以很容易地加入到系統中。
3.可以比較容易地設計一個組合命令。
4.調用同一方法實現不同的功能
缺點
使用命令模式可能會導致某些系統有過多的具體命令類。因為針對每一個命令都需要設計一個具體命令類,因此某些系統可能需要大量具體命令類,這將影響命令模式的使用。
歡迎關注我的視訊課程,地址如下,謝謝。
PHP物件導向設計模式