The PHP design pattern has been serialized for four phases. we know the proxy pattern, the responsibility chain pattern, and the structure Pattern. today we will discuss the Command pattern ), the command mode encapsulates a general operation mechanism. If you are familiar with C or PHP, you may have encountered PHP design patterns. we have serialized four phases. we know the proxy mode, responsibility chain mode, and structure mode; today we will discuss the Command mode, which 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.
498) this. style. width = 498; "border =" 0 "/>
Command mode in PHP design mode
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 Command takes action.
- * In this case the implementation must return a result,
- * Sometimes it only has side effects.
- */
- InterfaceValidator
- {
- /**
- * The method cocould have any parameters.
- * @ Param mixed
- * @ Return boolean
- */
- Public FunctionIsValid ($ value );
- }
-
- /**
- * ConcreteCommand.
- */
- ClassMoreThanZeroValidatorImplementsValidator
- {
- Public FunctionIsValid ($ value)
- {
- Return$ Value> 0;
- }
- }
-
- /**
- * ConcreteCommand.
- */
- ClassEvenValidatorImplementsValidator
- {
- Public FunctionIsValid ($ value)
- {
- Return$ Value % 2 = 0;
- }
- }
-
- /**
- * The Invoker. An implementation cocould store more than one
- * Validator if needed.
- */
- ClassArrayProcessor
- {
- ProtectedThe PHP design pattern has been serialized for four phases. we know the proxy pattern, the responsibility chain pattern, and the structure Pattern. today we will discuss the Command pattern ), 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.
498) this. style. width = 498; "border =" 0 "/>
Command mode in PHP design mode
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:
___FCKpd___0
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.
Rule;
-
- Public Function_ Construct (Validator $ rule)
- {
- $ This-> _ rule = $ rule;
- }
-
- Public FunctionProcess (Array$ Numbers)
- {
- Foreach($ NumbersAs$ N ){
- If($ This-> _ rule-> IsValid ($ n )){
- Echo $ n, "\ n ";
- }
- }
- }
- }
-
- // Client code
- $ Processor =NewArrayProcessor (NewEvenValidator ());
- $ 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.