An in-depth analysis of command patterns in PHP design patterns _php Skills

Source: Internet
Author: User

Command mode, which is the mechanism that encapsulates a common operation.

If you are familiar with C or PHP, you may have encountered the command, which is equivalent to a callback (callback) in the program. Callbacks typically use a function pointer or data structure such as a string and array implementation in PHP, which is an abstraction over a method invocation that absorbs all the object-oriented benefits: compositing, 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 function programming capabilities (closures) can be used as a local implementation of command mode, but using abstract data types for each command hierarchy helps type safety.



In this pattern, the invoker (caller) knows that the command passed to it does not have to rely on the real Concretecommand (specific command) implementation, resolving problems related to method invocation through configuration, such as UI control buttons and menus, and so on, referencing a command, Their behavior is presented through a common Concretecommand instance.
Participants:
command: Defines an abstraction above a method call;
Concretecommand (Specific command): an operation of the implementation;
Invoker (caller): Refers to the command instance as an action available to it.
The following code shows an example of a validator component implemented as a Command object:
Copy Code code as follows:

/**
* The Command abstraction.
* 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;
}
}

/**
* The invoker. An implementation could 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));

Some considerations for using the command pattern in PHP design mode:
Some parameters in the method invocation can be provided when constructing Concretecommand, and the original function is effectively locally applied (currying).
A command can be seen as a very simple strategy (strategy) with only one method, focusing on the operation of the object;
Concretecommands also organizes every resource they need to achieve their goals, primarily the receiver of behavior (the recipient), which invokes methods to execute a command;
Composite mode, decorative mode, and other modes can be combined with Command mode for more command, Decoration command, and so on.

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.