PHP design mode-command mode

Source: Internet
Author: User
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:

 
 
  1. /**
  2. * The Command takes action.
  3. * In this case the implementation must return a result,
  4. * Sometimes it only has side effects.
  5. */
  6. InterfaceValidator
  7. {
  8. /**
  9. * The method cocould have any parameters.
  10. * @ Param mixed
  11. * @ Return boolean
  12. */
  13. Public FunctionIsValid ($ value );
  14. }
  15.  
  16. /**
  17. * ConcreteCommand.
  18. */
  19. ClassMoreThanZeroValidatorImplementsValidator
  20. {
  21. Public FunctionIsValid ($ value)
  22. {
  23. Return$ Value> 0;
  24. }
  25. }
  26.  
  27. /**
  28. * ConcreteCommand.
  29. */
  30. ClassEvenValidatorImplementsValidator
  31. {
  32. Public FunctionIsValid ($ value)
  33. {
  34. Return$ Value % 2 = 0;
  35. }
  36. }
  37.  
  38. /**
  39. * The Invoker. An implementation cocould store more than one
  40. * Validator if needed.
  41. */
  42. ClassArrayProcessor
  43. {
  44. 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;
  45.  
  46. Public Function_ Construct (Validator $ rule)
  47. {
  48. $ This-> _ rule = $ rule;
  49. }
  50.  
  51. Public FunctionProcess (Array$ Numbers)
  52. {
  53. Foreach($ NumbersAs$ N ){
  54. If($ This-> _ rule-> IsValid ($ n )){
  55. Echo $ n, "\ n ";
  56. }
  57. }
  58. }
  59. }
  60.  
  61. // Client code
  62. $ Processor =NewArrayProcessor (NewEvenValidator ());
  63. $ 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.

Related Article

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.