Example of command mode usage for PHP design mode

Source: Internet
Author: User
Tags abstract

  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 ();?>  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.