Home > Backend Development > PHP Tutorial > An in-depth analysis of the command pattern of PHP design patterns_PHP tutorial

An in-depth analysis of the command pattern of PHP design patterns_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:07:05
Original
979 people have browsed it

Command mode (Command), command mode is a mechanism that encapsulates a common operation.

If you are familiar with C or PHP, you may have encountered Command, which is equivalent to: callback in the program. Callbacks are usually implemented using a function pointer or data structure such as strings and arrays in PHP. Command is an abstraction on top of a method call, which absorbs all the benefits of object-oriented: composition, inheritance and processing.

For example, the book "Design Patterns" recommends using Command to store user behavior chains to support undo and redo operations.

Note that PHP 5.3 functional programming capabilities (closures) can be treated as a native implementation of the Command pattern, but using abstract data types for each command hierarchy helps with type safety.



In this mode, the Invoker (caller) knows the Command passed to it, without relying on the real ConcreteCommand (specific command) implementation, solved Issues related to method calling through configuration, such as UI control buttons and menus refer to a Command, and their behavior is presented through a common ConcreteCommand instance.
Participants:
◆Command (command): Define an abstraction on top of a method call;
◆ConcreteCommand (concrete command) : The implementation of an operation;
◆Invoker (caller): refers to the Command instance as its available operation.
The following code shows an example of the Validator component implemented as a Command object:
Copy the code The code is as follows:

/** 
 * The Command abstraction. 
 * In this case the implementation must return a result, 
 * sometimes it only has side effects. 
 */
interface Validator
{
/** 
     * The method could 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; (Validator $rule)
{
$this->_rule = $rule;
}

public function process(array $numbers)
{ numbers as $n) {
                                                                            }
}

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


Some notes on using the command pattern in PHP design patterns:
◆ Certain parameters in the method call can be provided when constructing the ConcreteCommand, effectively applying locally (currying) original function;
◆A Command can be regarded as a very simple strategy with only one method, focusing on the operation of the object;
◆ConcreteCommands also need to organize every function they need A resource to achieve their goals, mainly the Receiver of behavior, they call methods to execute a Command;
◆ Composite mode, decoration mode and other modes can be combined with the command mode to obtain more Commands , Decoration Command and so on.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327559.htmlTechArticleCommand mode (Command), command mode is a mechanism that encapsulates a common operation. If you are familiar with C or PHP, you may have encountered Command, which is equivalent to: callback (callb...
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template