1. Encapsulate the request into an object, allowing different requests to be used to parameterize clients. Queue or log requests and support undoable actions.
2. Compose command abstract classes, concrete command classes, etc.
Command abstract class is used to declare the interface for performing operations.
A specific command class binds a receiver object to an action and calls the corresponding operation of the receiver.
The sender of the command, asking the command to execute this request.
A command receiver knows how to perform operations related to executing a request. Any class may serve as a receiver.
Client code that creates a specific command object and sets its receiver.
Example
<?php //Command abstract class abstract class Command{ //Execute method abstract function Excute(); } //Concrete command class - there can be multiple inherited concrete classes according to different situations class ConcreteCommand extends Command{ private $Receiver; function __construct(Receiver $Receiver) { $this->Receiver = $Receiver; } functionExcute() { $this->Receiver->DoSomething(); } } //Receiver class class Receiver{ //Define what the receiver needs to do, there can be many functions DoSomething() { echo "Receiver do something."; } } //Invoker class Invoker{ private $Command; function __construct(Command $Command) { $this->Command = $Command; } functionAction() { $this->Command->Excute(); } } //Call//Call without using the caller class $Receiver = new Receiver(); $Command = new ConcreteCommand($Receiver); $Command->Excute(); //Use the caller class $Invoker = new Invoker($Command); $Invoker->Action(); ?>
The above is the understanding of PHP command mode, I hope it will be helpful to everyone.