Learn PHP Design Patterns PHP Implementation Command mode (commands) _php tips

Source: Internet
Author: User
Tags learn php

I. Intention
Encapsulates a request as an object, using a different request to parameterize the client, queuing or logging request logs for requests, and supporting actions that can be undone.
The variable aspect is: When, how to meet a request
The command mode is the encapsulation of the command. The command mode divides the responsibility for issuing the command from the responsibility of executing the command and delegating it to different objects.
The requesting party makes a request to perform an action, the receiving party receives the request, and performs the action. The command mode allows the requesting party to be independent of the receiving party, making it unnecessary for the requesting party to know the interface of the party receiving the request, not to mention how the request was received, and whether the operation was executed, when it was executed, and how it was executed.
Second, the command mode structure diagram

iii. key roles in command mode
Command role: Declares an abstract interface to all specific command classes. This is an abstract role.
The specific command (Concretecommand) role: Defines a weak coupling between the receiver and the behavior, implements the Execute () method, and is responsible for invoking the corresponding operation to receive the test. The Execute () method is often called an execution method.
Client role: Creates a specific command (Concretecommand) object and determines its recipient.
Requester (Invoker) role: is responsible for invoking the Command object execution request, the related method is called the action method.
Recipient (Receiver) role: be responsible for implementing and executing a request specifically. Any class can be a receiver, and the method of implementing and executing the request is called the action method.
Four, the advantage of command mode
Advantages of Command mode:
1. The command mode leaves the object requesting an action with the object that knows how to perform an operation.
2. The command class, like any other class, can be modified and promoted.
3. The command object can be aggregated together and synthesized into a synthesis command.
4, you can easily add a new command class.
The disadvantage of command mode: It may cause some systems to have too many specific command classes.
Five, the command mode to apply the scene
1. Abstract the action to be performed to parameterize the object. Command mode is an object-oriented alternative to the callback mechanism.
2. Specify, arrange and execute requests at different times.
3, support cancel operation.
4, support the change log.
5. Constructs a system with the high-level operation built on the primitive language operation. Command mode provides a way to model transactions. command has a common interface so that you can invoke all transactions in the same way. Using this pattern at the same time is also easy to add new transactions to extend the system.
vi. Command mode and other modes
Compositing mode (composite mode): Composite mode can be implemented Macro command
Prototype mode (prototype mode): If the command class has a clone (or the Copy method mentioned in the previous article) method, the command can be replicated. This pattern may be required to replicate a command object in the current state in order mode to support multiple cancellation operations
Seven, Command mode PHP sample

<?php/** * Command role/interface command {/** * Execute method */Public function execute ();}
 
  /** * Specific Command role * * Class Concretecommand implements command {private $_receiver;
  Public function __construct (Receiver $receiver) {$this->_receiver = $receiver;
  /** * Execute method/Public function execute () {$this->_receiver->action ();
 
  }/** * Receiver role */class Receiver {/* Receiver name/private $_name;
  Public function __construct ($name) {$this->_name = $name;
  /** * Action method/Public Function action () {echo $this->_name, ' do action.<br/> ';
 
  }/** * Requester role/class Invoker {private $_command;
  Public function __construct (Command $command) {$this->_command = $command;
  The Public Function action () {$this->_command->execute ();
   }/** * Client/class Client {/** * Main program. */public static function main () {$receiver = new receiver (' Phpppan ');
    $command = new Concretecommand ($receiver);
    $invoker = new Invoker ($command);
  $invoker->action ();
 
} client::main (); ?>


Eight, Command mode collaboration
1, Client creates a Concretecommand object and specifies its receiver object
2, A Invoker object stores the Concretecommand object
3, which submits a request by calling the command object's execute operation. If the command is revocable, Concretecommand stores the current state before the execute operation to cancel the command. The
4, Concretecommand object swap Some of its receiver operations to execute the request.
Nine, macro commands
here, let's take a simple add-and-paste feature to make a macro command of these two commands.
We can create a new copy command and Paste command, and then add it to the Macro command.
The code shown below:

<?php/** * Command role/interface command {/** * Execute method */Public function execute ();}
   /** * Macro Command interface/interface Macrocommand extends command {/** * macro commands the management method of aggregation, you can delete a member command * @param commands $command
 
  * * Public Function remove (Command $command);
 
/** * Macro Command Aggregation management method, you can add one member command * @param command $command/Public function Add (command $command);
 
  Class Demomacrocommand implements Macrocommand {private $_commandlist;
  Public Function __construct () {$this->_commandlist = array ();
    The Public function remove (Command $command) {$key = Array_search ($command, $this->_commandlist);
    if ($key = = False) {return false;
    } unset ($this->_commandlist[$key]);
  return TRUE;
  The Public function Add (Command $command) {return Array_push ($this->_commandlist, $command);
    The Public Function execute () {foreach ($this->_commandlist as $command) {$command->execute ();
}
 
  } 
/** * Specific Copy command role * * Class Copycommand implements command {private $_receiver;
  Public function __construct (Receiver $receiver) {$this->_receiver = $receiver;
  /** * Execute method/Public function execute () {$this->_receiver->copy ();
 
  }/** * Specific copy command role * * Class Pastecommand implements command {private $_receiver;
  Public function __construct (Receiver $receiver) {$this->_receiver = $receiver;
  /** * Execute method/Public function execute () {$this->_receiver->paste ();
 
  }/** * Receiver role */class Receiver {/* Receiver name/private $_name;
  Public function __construct ($name) {$this->_name = $name;
  /** * Copy method/Public function copy () {echo $this->_name, ' do copy action.<br/> ';
  /** * Paste Method/Public Function paste () {echo $this->_name, ' Do paste action.<br/> ';
 
  }/** * Requester role/class Invoker {private $_command; Public function __construct (Command $command) {$this->_command = $command;
  The Public Function action () {$this->_command->execute ();
   }/** * Client/class Client {/** * Main program.
    * * public static function main () {$receiver = new receiver (' Phpppan ');
    $pasteCommand = new Pastecommand ($receiver);
 
    $copyCommand = new Copycommand ($receiver);
    $macroCommand = new Demomacrocommand ();
    $macroCommand->add ($copyCommand);
 
    $macroCommand->add ($pasteCommand);
    $invoker = new Invoker ($macroCommand);
 
    $invoker->action ();
    $macroCommand->remove ($copyCommand);
    $invoker = new Invoker ($macroCommand);
  $invoker->action ();
 
 
} client::main (); ?>

This is the code that uses PHP to implement command mode , and there are some conceptual distinctions about command patterns that I hope will help you learn.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.