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.
http://www.bkjia.com/PHPjc/327559.htmlwww.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...