命令模式,也稱爲動做或者事務模式,不少教材會用飯館來舉例。做爲顧客的咱們是命令的下達者,服務員是這個命令的接收者,菜單是這個實際的命令,而廚師是這個命令的執行者。那麼,這個模式解決了什麼呢?當你要修改菜單的時候,只須要和服務員說就行了,她會轉達給廚師,也就是說,咱們實現了顧客和廚師的解耦。也就是調用者與實現者的解耦。固然,不少設計模式能夠作到這一點,可是命令模式可以作到的是讓一個命令接收者實現多個命令(服務員下單、拿酒水、上菜),或者把一條命令轉達給多個實現者(熱菜廚師、涼菜廚師、主食師傅)。這纔是命令模式真正發揮的地方!!php
GoF定義:將一個請求封裝爲一個對象,從而使你可用不一樣的請求對客戶進行參數化;對請求排隊或記錄請求日誌,以及支持可撤消的操做git
GoF類圖github
代碼實現設計模式
class Invoker { public $command; public function __construct($command) { $this->command = $command; } public function exec() { $this->command->execute(); } }
首先咱們定義一個命令的接收者,或者說是命令的請求者更恰當。類圖中的英文定義這個單詞是「祈求者」。也就是由它來發起和操做命令。微信
abstract class Command { protected $receiver; public function __construct(Receiver $receiver) { $this->receiver = $receiver; } abstract public function execute(); } class ConcreteCommand extends Command { public function execute() { $this->receiver->action(); } }
接下來是命令,也就是咱們的「菜單」。這個命令的做用是爲了定義真正的執行者是誰。框架
class Receiver { public $name; public function __construct($name) { $this->name = $name; } public function action() { echo $this->name . '命令執行了!', PHP_EOL; } }
接管者,也就是執行者,真正去執行命令的人。學習
// 準備執行者 $receiverA = new Receiver('A'); // 準備命令 $command = new ConcreteCommand($receiverA); // 請求者 $invoker = new Invoker($command); $invoker->exec();
客戶端的調用,咱們要聯繫好執行者也就是挑有好廚子的飯館(Receiver),而後準備好命令也就是菜單(Command),最後交給服務員(Invoker)。this
完整代碼:https://github.com/zhangyue0503/designpatterns-php/blob/master/09.command/source/command.php設計
<?php class Invoker { private $command = []; public function setCommand(Command $command) { $this->command[] = $command; } public function exec() { if(count($this->command) > 0){ foreach ($this->command as $command) { $command->execute(); } } } public function undo() { if(count($this->command) > 0){ foreach ($this->command as $command) { $command->undo(); } } } } abstract class Command { protected $receiver; protected $state; protected $name; public function __construct(Receiver $receiver, $name) { $this->receiver = $receiver; $this->name = $name; } abstract public function execute(); } class ConcreteCommand extends Command { public function execute() { if (!$this->state || $this->state == 2) { $this->receiver->action(); $this->state = 1; } else { echo $this->name . '命令正在執行,沒法再次執行了!', PHP_EOL; } } public function undo() { if ($this->state == 1) { $this->receiver->undo(); $this->state = 2; } else { echo $this->name . '命令未執行,沒法撤銷了!', PHP_EOL; } } } class Receiver { public $name; public function __construct($name) { $this->name = $name; } public function action() { echo $this->name . '命令執行了!', PHP_EOL; } public function undo() { echo $this->name . '命令撤銷了!', PHP_EOL; } } // 準備執行者 $receiverA = new Receiver('A'); $receiverB = new Receiver('B'); $receiverC = new Receiver('C'); // 準備命令 $commandOne = new ConcreteCommand($receiverA, 'A'); $commandTwo = new ConcreteCommand($receiverA, 'B'); $commandThree = new ConcreteCommand($receiverA, 'C'); // 請求者 $invoker = new Invoker(); $invoker->setCommand($commandOne); $invoker->setCommand($commandTwo); $invoker->setCommand($commandThree); $invoker->exec(); $invoker->undo(); // 新加一個單獨的執行者,只執行一個命令 $invokerA = new Invoker(); $invokerA->setCommand($commandOne); $invokerA->exec(); // 命令A已經執行了,再次執行所有的命令執行者,A命令的state判斷沒法生效 $invoker->exec();
咱們的手機工廠和餐廳其實並無什麼兩樣,當咱們須要代工廠來製做手機時,也是先下訂單,這個訂單就能夠看作是命令。在這個訂單中,咱們會規定好須要用到的配件,什麼型號的CPU,什麼型號的內存,預裝什麼系統之類的。而後代工廠的工人們就會根據這個訂單來進行生產。在這個過程當中,我不用關心是某一個工人仍是一羣工人來執行這個訂單,我只須要將這個訂單交給和咱們對接的人就能夠了,而後只管等着手機生產出來進行驗收咯!!日誌
完整代碼:https://github.com/zhangyue0503/designpatterns-php/blob/master/09.command/source/command-up.php
短信功能又回來了,咱們發現除了工廠模式外,命令模式貌似也是一種不錯的實現方式哦。在這裏,咱們依然是使用那幾個短信和推送的接口,話很少說,咱們用命令模式再來實現一個吧。固然,有興趣的朋友能夠接着實現咱們的短信撤回功能哈,想一想上面的命令取消是怎麼實現的。
短信發送類圖
完整源碼:https://github.com/zhangyue0503/designpatterns-php/blob/master/09.command/source/command-message.php
<?php class SendMsg { private $command = []; public function setCommand(Command $command) { $this->command[] = $command; } public function send($msg) { foreach ($this->command as $command) { $command->execute($msg); } } } abstract class Command { protected $receiver = []; public function setReceiver($receiver) { $this->receiver[] = $receiver; } abstract public function execute($msg); } class SendAliYun extends Command { public function execute($msg) { foreach ($this->receiver as $receiver) { $receiver->action($msg); } } } class SendJiGuang extends Command { public function execute($msg) { foreach ($this->receiver as $receiver) { $receiver->action($msg); } } } class SendAliYunMsg { public function action($msg) { echo '【阿X雲短信】發送:' . $msg, PHP_EOL; } } class SendAliYunPush { public function action($msg) { echo '【阿X雲推送】發送:' . $msg, PHP_EOL; } } class SendJiGuangMsg { public function action($msg) { echo '【極X短信】發送:' . $msg, PHP_EOL; } } class SendJiGuangPush { public function action($msg) { echo '【極X推送】發送:' . $msg, PHP_EOL; } } $aliMsg = new SendAliYunMsg(); $aliPush = new SendAliYunPush(); $jgMsg = new SendJiGuangMsg(); $jgPush = new SendJiGuangPush(); $sendAliYun = new SendAliYun(); $sendAliYun->setReceiver($aliMsg); $sendAliYun->setReceiver($aliPush); $sendJiGuang = new SendJiGuang(); $sendAliYun->setReceiver($jgMsg); $sendAliYun->setReceiver($jgPush); $sendMsg = new SendMsg(); $sendMsg->setCommand($sendAliYun); $sendMsg->setCommand($sendJiGuang); $sendMsg->send('此次要搞個大活動,快來註冊吧!!');
說明
關注公衆號:【硬核項目經理】獲取最新文章
添加微信/QQ好友:【xiaoyuezigonggong/149844827】免費得PHP、項目管理學習資料
知乎、公衆號、抖音、頭條搜索【硬核項目經理】
B站ID:482780532