php command mode implementation

php command mode implementation


Command pattern: encapsulate a request as an object, so that we can parameterize the customer with different requests; Queue requests or record request logs, and support revocable operations. Command mode is an object behavior mode, which is also called action mode or transaction mode.

Take the motorcycle factory as an example, the workshop director needs to assign production orders to the production line. This can be implemented in command mode

Pattern structure

  1. Command - command interface
  2. Invoker - command dispatcher
  3. workShop - the production line accepts the command
  4. workShopDirector - workshop director - assign orders to the production line

UML legend

Code example


//Command interface
interface Command
    public function execute();
//Command dispatcher
class Invoker
    //Command list
    private $_command = array();
    //New command
    public function addCommand($command)
        $this->_command[] = $command;
    //Execute command
    public function executeCommand()
        foreach ($this->_command as $command) {
    //Remove command
    public function removeCommand($command)
        $key = array_search($command, $this->_command);
        if ($key !== false) {
//The production line accepts the order
class workShop
    private $_production_line  = null;

    public function __construct($productionLine) {
        $this->_production_line = $productionLine;
    public function action()
         echo $this->_production_line." Execute the attack command and start assembling the motorcycle<br />";
//Workshop director - assign orders to the production line
class workShopDirector implements Command{
    private $_work_shop;
    public function __construct($_work_shop)
          $this->_work_shop = $_work_shop;
    //Execute command
    public function execute()
$workShopA = new workShop("Production line 1");
$workShopB = new workShop("Production line 2");
$workShopDirectorA = new workShopDirector($workShopA);
$workShopDirectorB = new workShopDirector($workShopB);

$objInvoker = new Invoker();

pattern analysis

The essence of command mode is to encapsulate the command, separating the responsibility of issuing the command from the responsibility of executing the command.

  • Each command is an operation: the requesting party sends 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 have to know the interface of the receiving party, let alone how the request is received, whether, when and how the operation is executed.
  • Command mode makes the request itself an object that can be stored and passed like other objects.
  • The key of the command mode is to introduce the abstract command interface, and the sender programs for the abstract command interface. Only the specific command that implements the abstract command interface can be associated with the receiver.

Advantages and disadvantages

Advantages of command mode

  • Reduce the coupling of the system.
  • New commands can be easily added to the system.
  • You can easily design a command queue and macro commands (combined commands).
  • Undo and Redo requests can be easily implemented.

Disadvantages of command mode

  • Using command mode may cause some systems to have too many specific command classes. Because a specific command class needs to be designed for each command, some systems may need a large number of specific command classes, which will affect the use of command mode.

Applicable environment

Command mode can be used when:

  • The system needs to decouple the request caller and the request receiver so that the caller and the receiver do not interact directly.
  • The system needs to specify, queue and execute requests at different times.
  • The system needs to support Undo and redo operations of commands.
  • The system needs to combine a set of operations, that is, to support macro commands

Tags: PHP Design Pattern

Posted on Mon, 13 Sep 2021 16:33:35 -0400 by bawla