This article mainly introduces the command mode usage example of PHP design pattern, the command mode encapsulates a request into an object so that you can parameterize the client with different requests, queue or log request logs for requests, and support revocable operations
Command class: 1. Command role: declares an abstract interface to all specific command classes. This is an abstract role. 2. Specific command role: Defines a weak coupling between the recipient and the behavior; Implements the Execute method, which is responsible for invoking the appropriate action that is accepted. The Execute () method is often called execution Method 3. Customer role: Create a specific Command object and determine its recipient. 4. Requestor role: Responsible for invoking the Command object execution request, the related method is called the action method. 5. Recipient role: Responsible for the specific implementation and implementation of a request. Function: 1. Abstract the action to be performed to parameterize the object. 2. Specify, arrange, and execute requests at different times. 3. Support Cancel Operation 4. Support for modification of log copy code code as follows: <?php//Command Interface interface command{ public function Execute ( ); }//Specific command class Concretecommand implements command{ private $_receiver; public func tion __construct ($receiver) { $this->_receiver = $receiver; } &nb Sp public function Execute () { $this->_receiver->action (); &NBSP ; &NBSP} //Recipient class receiver{ private $_name; public function __construct ($name) { $this->_name = $name &NBSP} //action method &NBS P public function action () { $this->_name. ' Do action .<br/> '; }//Requestor class invoker{ private $_command public function __construct ($command) { $this->_command = $command; &NBSP public function action () { $this->_command- >execute (); &NBSP} //client class client{ public static function main () { $receiver = new Receiver (' Jaky '); $command = new Concreteecommand ($receiver); $invoker = new Invoker ($command); $invoker->action (); &NBSP} client::main ();?>