Command mode:
Command pattern: Encapsulates a request as an object, allowing us to parameterize the client with different requests, queue requests or log requests, and support revocable operations. The command pattern is an object-oriented pattern with an alias of Action (action) mode or transaction (Transaction) mode.
Mode motive:
In software design, we often need to send requests to certain objects, but we do not know who the recipient of the request is, and we do not know what the requested operation is, we just need to specify a specific request receiver when the program is running, and then we can use the command mode to design it. This allows the request sender and the requesting recipient to eliminate the coupling between each other, making the call relationship between objects more flexible.
The command pattern can be completely decoupled from the sender and receiver, and there is no direct reference between the sender and the recipient, and the object sending the request needs only to know how to send the request, without having to know how to complete the request. This is the mode motive for the command pattern.
The command pattern contains the following roles:
Command: Abstract Commands class
Concretecommand: Specific Command class
Invoker: Caller
Receiver: Receiver
Client: Customer Class
UML Class Diagrams:
Code implementation:
<?PHPHeader("content-type:text/html; Charset=utf-8 ");//command interface, defining the execution function to be implemented by the specific command interfaceInterfacecommand{functionexecute ();}//specific command role Attackcommand, specifying the recipient to execute the attack commandclassAttackcommandImplementscommand{Private $receiver; function__construct (Receiver$receiver){ $this->receiver =$receiver; } functionExecute () {$this->receiver->attackaction (); }}//specific command role Defensecommand, specifying the recipient to perform defensive commandsclassDefensecommandImplementscommand{Private $receiver; function__construct (Receiver$receiver){ $this->receiver =$receiver; } functionExecute () {$this->receiver->defenseaction (); }}//recipient, command to perform a specific command roleclassreceiver{Private $name; function__construct ($name){ $this->name =$name; } functionattackaction () {Echo $this->name. " Execution of the attack command "; } functiondefenseaction () {Echo $this->name. " Executed a defensive command "; }}//Requester , requesting execution of a specific commandclassinvoker{Private $concreteCommand; function__construct ($concreteCommand){ $this->concretecommand =$concreteCommand; } functionExecuteCommand () {$this->concretecommand->execute (); }}//Client Roleclassclient{function__construct () {$receiverZhao=NewReceiver ("Zhao Zhi Days"); $attackCommand=NewAttackcommand ($receiverZhao); $attackInvoker=NewInvoker ($attackCommand); $attackInvoker-ExecuteCommand (); $receiverYe=NewReceiver ("Ye Liangchen"); $defenseCommand=NewDefensecommand ($receiverYe); $defenseInvoker=NewInvoker ($defenseCommand); $defenseInvoker-ExecuteCommand (); }}//Test:NewClient ();
Pattern Analysis:
The essence of the command pattern is to encapsulate the command, separating the responsibility for issuing the command from the responsibility for executing the command.
Each command is an action: the requesting party makes a request, requests an action, the receiving party receives the request, and executes the action. The
command pattern allows the requesting party and the receiving party to be independent, so that the requesting party does not have to know the interface of the party receiving the request, not to know how the request was received, and whether the operation was executed, when it was executed, and how it was executed. The
command pattern makes the request itself an object that can be stored and passed just like any other object. The key to the
command pattern is the introduction of an abstract command interface, and the sender is programmed for an abstract command interface, and only specific commands that implement an abstract command interface can be associated with the receiver.
Applicability:
Abstract the action to be executed to parameterize an object, you can use the callback (call back) function in the process language to express this parameterization mechanism. A callback function is a function that registers somewhere before it is called at a later time when it is needed. The Command pattern is an object-oriented alternative to the callback mechanism.
Specify, arrange, and execute requests at different times. A command object can have a lifetime that is independent of the initial request. If the recipient of a request can be expressed in an address space-independent manner, the Command object responsible for the request is routed to a different process and the request is implemented there.
Support Cancel operation. The command's Excute operation stores the state before the operation is implemented, which is used to eliminate the effect of the operation when the operation is canceled. The Command interface must add an Unexecute action that cancels the effect of the last execute call. The commands executed are stored in a history list. You can iterate through this list backwards and forwards and invoke Unexecute and execute separately to achieve the infinite number of "undo" and "Redo".
Support for modifying the log so that when the system crashes, these modifications can be re-done again. Adding mount operations and storage operations to the command interface can be used to maintain a consistent change log for changes. The process of recovering from a crash involves re-reading the recorded commands from disk and re-executing them with the execute operation.
Construct a system with high-level operations built on primitive operations. Such a structure is common in information systems that support transactions (transaction). A transaction encapsulates a set of changes to the data. The command pattern provides a way to model transactions. command has a common interface that allows you to invoke all transactions in the same way. It is also easy to add new transactions to extend the system by using this mode.
Command mode and PHP implementation