In the previous article "Understanding the Prototype Mode in PHP" we introduced the prototype mode in PHP. This article will take you to understand the command mode in PHP.
Command mode, also known as action or transaction mode, many textbooks will use restaurants as an example. As customers, we are the order givers, the waiters are the recipients of this order, the menu is the actual order, and the chef is the executor of this order.
So, what does this model solve? When you want to modify the menu, you only need to tell the waiter and she will convey it to the chef. In other words, we have achieved the decoupling of customers and chefs. That is the decoupling of callers and implementers.
Of course, many design patterns can do this, but what the command pattern can do is to let a command receiver implement multiple commands (the waiter places an order, gets drinks, serves food), or puts a command Relayed to multiple implementers (hot cooks, cold cooks, main course chefs). This is where the command pattern really comes into play! !
GoF definition: Encapsulate a request as an object, so that you can use different requests to the customer Parameterization; queuing or logging requests, and supporting undoable operations
GoF Class Diagram
Code implementation
class Invoker { public $command; public function __construct($command) { $this->command = $command; } public function exec() { $this->command->execute(); } }
First we define a receiver of a command, or more appropriately, a requester of a command. The English definition of this word in the class diagram is "supplicant". That is, it initiates and operates commands.
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(); } }
The next step is the command, which is our "menu". The purpose of this command is to define who the real executor is.
class Receiver { public $name; public function __construct($name) { $this->name = $name; } public function action() { echo $this->name . '命令执行了!', PHP_EOL; } }
The taker, that is, the executor, is the person who actually executes the order.
// 准备执行者 $receiverA = new Receiver('A'); // 准备命令 $command = new ConcreteCommand($receiverA); // 请求者 $invoker = new Invoker($command); $invoker->exec();
To call the client, we need to contact the executor, that is, select a restaurant with a good chef (Receiver), and then prepare the order, which is the menu (Command), and finally hand it to the waiter (Invoker).
Full code: 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();
Our mobile phone factories and restaurants are actually There is no difference. When we need a foundry to make a mobile phone, we also place an order first. This order can be regarded as an order. In this order, we will specify the accessories that need to be used, what type of CPU, what type of memory, what system is pre-installed, etc. Then the workers in the foundry will produce according to this order. In this process, I don’t have to worry about whether a certain worker or a group of workers executes the order. I only need to hand over the order to the person who interfaces with us, and then just wait for the mobile phone to be produced for acceptance! !
https://github.com/zhangyue0503/designpatterns-php/blob/master/09.command/source/command-up.php
The SMS function is back. We found that in addition to the factory mode, the command mode seems to be a good way to implement it. Here, we are still using those SMS and push interfaces. Without further ado, let’s implement another one using command mode. Of course, interested friends can then implement our SMS withdrawal function. Think about how the above command cancellation is implemented.
SMS sending class diagram
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('这次要搞个大活动,快来注册吧!!');
Description
Original address: https://juejin .cn/post/6844903950768930823
Author: Hardcore Project Manager
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of An in-depth analysis of the command pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!