Home  >  Article  >  Backend Development  >  Learn PHP design patterns PHP implements command mode (command)_php skills

Learn PHP design patterns PHP implements command mode (command)_php skills

WBOY
WBOYOriginal
2016-05-16 20:03:30956browse

1. Intention
Encapsulate a request as an object, allowing you to parameterize clients with different requests; queue or log requests; and support undoable operations.
The variable aspect is: when and how to satisfy a request
Command mode is an encapsulation of commands. The command pattern separates the responsibility for issuing commands and the responsibility for executing commands, and delegates them to different objects.
The requesting party issues a request to perform an operation; the receiving party receives the request and performs the operation. The command mode allows the requesting party and the receiving party to be independent, so that the requesting party does not need to know the interface of the party receiving the request, let alone how the request is received, whether, when and how the operation is executed. executed.
2. Command mode structure diagram

3. Main characters in command mode
Command role: declares an abstract interface for all specific command classes. This is an abstract role.
ConcreteCommand role: Define a weak coupling between the recipient and the behavior; implement the Execute() method, which is responsible for calling the corresponding operation of the receiving test. The Execute() method is usually called the execution method.
Client role: Creates a ConcreteCommand object and determines its recipient.
Requester (Invoker) role: Responsible for calling the command object to execute the request. The related method is called the action method.
Receiver role: Responsible for implementing and executing a request. Any class can become a receiver, and the method of implementing and executing the request is called an action method.
4. Advantages of command mode
Advantages of command mode:
1. The command pattern separates the object that requests an operation from the object that knows how to perform an operation.
2. Command classes, like any other classes, can be modified and promoted.
3. Command objects can be aggregated together and synthesized into composite commands.
4. New command classes can be easily added.
Disadvantages of command mode: It may lead to too many specific command classes for some systems.
5. Applicable scenarios of command mode
1. Abstract the action to be performed to parameterize the object. The Command pattern is an object-oriented alternative to the callback mechanism.
2. Specify, sequence and execute requests at different times.
3. Support cancellation operation.
4. Support modification log.
5. Construct a system with high-level operations built on primitive operations. Command pattern provides a way to model transactions. Command has a public interface that allows you to call all transactions in the same way. It is also easy to add new transactions to expand the system using this mode.
6. Command mode and other modes
Composite mode (composite mode): Composite mode can be implemented with macro commands
Prototype mode: If the command class has a clone (or copy method mentioned in the previous article) method, the command can be copied. This mode may be needed when the command mode supports multiple cancellation operations to copy the current state of the Command object
7. Command mode PHP example

<&#63;php
/**
 * 命令角色
 */
interface Command {
 
  /**
   * 执行方法
   */
  public function execute();
}
 
/**
 * 具体命令角色
 */
class ConcreteCommand implements Command {
 
  private $_receiver;
 
  public function __construct(Receiver $receiver) {
    $this->_receiver = $receiver;
  }
 
  /**
   * 执行方法
   */
  public function execute() {
    $this->_receiver->action();
  }
}
 
/**
 * 接收者角色
 */
class Receiver {
 
  /* 接收者名称 */
  private $_name;
 
  public function __construct($name) {
    $this->_name = $name;
  }
 
  /**
   * 行动方法
   */
  public function action() {
    echo $this->_name, ' do action.<br />';
  }
}
 
/**
 * 请求者角色
 */
class Invoker {
 
  private $_command;
 
  public function __construct(Command $command) {
    $this->_command = $command;
  }
 
  public function action() {
    $this->_command->execute();
  }
}
 
/**
 * 客户端
 */
class Client {
 
   /**
   * Main program.
   */
  public static function main() {
    $receiver = new Receiver('phpppan');
    $command = new ConcreteCommand($receiver);
    $invoker = new Invoker($command);
    $invoker->action();
  }
}
 
Client::main();
 
&#63;>


8. Command mode collaboration
1. Client creates a ConcreteCommand object and specifies its Receiver object
2. An Invoker object stores the ConcreteCommand object
3. The Invoker submits a request by calling the execute operation of the Command object. If the command is undoable, ConcreteCommand stores the current state before executing the execute operation for use in canceling the command.
4. The ConcreteCommand object performs some operations on the Receiver that calls it to execute the request.
9. Macro Commands
Here, we take a simple add and paste function as an example, and combine these two commands into a macro command.
We can create new copy commands and paste commands, and then add them to macro commands.
The code is as follows:

<&#63;php
/**
 * 命令角色
 */
interface Command {
 
  /**
   * 执行方法
   */
  public function execute();
}
 
/**
 * 宏命令接口
 */
interface MacroCommand extends Command {
 
  /**
   * 宏命令聚集的管理方法,可以删除一个成员命令
   * @param Command $command
   */
  public function remove(Command $command);
 
  /**
   * 宏命令聚集的管理方法,可以增加一个成员命令
   * @param Command $command
   */
  public function add(Command $command);
 
}
 
 
class DemoMacroCommand implements MacroCommand {
 
  private $_commandList;
 
  public function __construct() {
    $this->_commandList = array();
  }
 
  public function remove(Command $command) {
    $key = array_search($command, $this->_commandList);
    if ($key === FALSE) {
      return FALSE;
    }
 
    unset($this->_commandList[$key]);
    return TRUE;
  }
 
  public function add(Command $command) {
    return array_push($this->_commandList, $command);
  }
 
  public function execute() {
    foreach ($this->_commandList as $command) {
      $command->execute();
    }
 
  }
 
}
 
/**
 * 具体拷贝命令角色
 */
class CopyCommand implements Command {
 
  private $_receiver;
 
  public function __construct(Receiver $receiver) {
    $this->_receiver = $receiver;
  }
 
  /**
   * 执行方法
   */
  public function execute() {
    $this->_receiver->copy();
  }
}
 
/**
 * 具体拷贝命令角色
 */
class PasteCommand implements Command {
 
  private $_receiver;
 
  public function __construct(Receiver $receiver) {
    $this->_receiver = $receiver;
  }
 
  /**
   * 执行方法
   */
  public function execute() {
    $this->_receiver->paste();
  }
}
 
/**
 * 接收者角色
 */
class Receiver {
 
  /* 接收者名称 */
  private $_name;
 
  public function __construct($name) {
    $this->_name = $name;
  }
 
  /**
   * 复制方法
   */
  public function copy() {
    echo $this->_name, ' do copy action.<br />';
  }
 
  /**
   * 粘贴方法
   */
  public function paste() {
    echo $this->_name, ' do paste action.<br />';
  }
}
 
/**
 * 请求者角色
 */
class Invoker {
 
  private $_command;
 
  public function __construct(Command $command) {
    $this->_command = $command;
  }
 
  public function action() {
    $this->_command->execute();
  }
}
 
/**
 * 客户端
 */
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();
 
 
&#63;>

The above is the code to implement command mode using PHP. There are also some conceptual distinctions about command mode. I hope it will be helpful to everyone's learning.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn