In-depth analysis of the command mode in the PHP design mode _ PHP Tutorial

Source: Internet
Author: User
In-depth analysis of the command mode in PHP design mode. The Command mode encapsulates a general operation mechanism. If you are familiar with C or PHP, you may have encountered Command, which is equivalent to the callback (callb Command mode (Command) in the program. The Command mode encapsulates a general operation mechanism.

If you are familiar with C or PHP, you may have encountered Command, which is equivalent to the callback in the program ). A callback usually uses a function pointer or data structure such as a string or array in PHP for implementation. Command is an abstraction above a method call, which absorbs all the benefits of object-oriented: merging, inheritance, and processing.

For example, in design patterns, we recommend using Command to store user behavior chains to support undo and redo operations.

Note that PHP 5.3 Function programming capabilities (closures) can be used as a local implementation of the Command mode, but using abstract data types for each Command hierarchy helps type security.



In this mode, Invoker (caller) knows the Command passed to it, and does not need to rely on the actual ConcreteCommand (specific Command) implementation. it solves the problems related to method calling through configuration, for example, the UI control buttons and menus reference a Command, and their behavior is presented through a common ConcreteCommand instance.
Participants:
◆ Command: defines an abstraction over a method call;
◆ ConcreteCommand (specific command): implementation of an operation;
◆ Invoker (caller): refers to the Command instance for its available operations.
The following code demonstrates the implementation of the Validator component as a Command object:

The code is as follows:


/**
* The Command takes action.
* In this case the implementation must return a result,
* Sometimes it only has side effects.
*/
Interface Validator
{
/**
* The method cocould have any parameters.
* @ Param mixed
* @ Return boolean
*/
Public function isValid ($ value );
}

/**
* ConcreteCommand.
*/
Class MoreThanZeroValidator implements Validator
{
Public function isValid ($ value)
{
Return $ value> 0;
}
}

/**
* ConcreteCommand.
*/
Class EvenValidator implements Validator
{
Public function isValid ($ value)
{
Return $ value % 2 = 0;
}
}

/**
* The Invoker. An implementation cocould store more than one
* Validator if needed.
*/
Class ArrayProcessor
{
Protected $ _ rule;

Public function _ construct (Validator $ rule)
{
$ This-> _ rule = $ rule;
}

Public function process (array $ numbers)
{
Foreach ($ numbers as $ n ){
If ($ this-> _ rule-> IsValid ($ n )){
Echo $ n, "\ n ";
}
}
}
}

// Client code
$ Processor = new ArrayProcessor (new EvenValidator ());
$ Processor-> process (array (1, 20, 18, 5, 0, 31, 42 ));


Precautions for using the command mode in PHP design mode:
◆ Some parameters in a method call can be provided when ConcreteCommand is constructed, and the original function (currying) is effectively applied;
◆ A Command can be regarded as a very simple Strategy with only one method (Strategy), focusing on object operations;
◆ ConcreteCommands also organizes every resource they need to achieve their goals, mainly the Receiver of the behavior. they call a method to execute a Command;
◆ Compound mode. the decoration mode and other modes can be combined with the Command mode to obtain more commands and decorative commands.

The Command mode encapsulates a mechanism for common operations. If you are familiar with C or PHP, you may have encountered Command, which is equivalent to: Callback (callb...

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.