PHP design mode-command mode

Source: Internet
Author: User
Pass requests from the client into an object so that you can parameterize the customer with different requests. This function is used to decouple "behavior requestor" from "behavior implementer" to achieve loose coupling between the two to adapt to changes.

Concept

Pass requests from the client into an object so that you can parameterize the customer with different requests. This function is used to decouple "behavior requestor" from "behavior implementer" to achieve loose coupling between the two to adapt to changes.

Role

Command: defines an abstraction over a method call;

ConcreteCommand (specific command): implementation of an operation;

Invoker: refers to the Command instance for its available operations.

Code

The code is as follows:

 0 ;}} /*** Class ConcreteCommandTwo Command 2 */class ConcreteCommandTwo implements Command {/*** implementation verification method ** @ param $ value ** @ return bool */public function isValid ($ value) {// return $ value % 2 = 0;}/*** Class Invoker caller */class Invoker {protected $ _ rule; /*** constructor * receives the specific command object * Invoker. ** @ param Command $ rule */public function _ construct (Command $ rule) {$ this-> _ rule = $ rule;} public function process (array $ numbers) {foreach ($ numbers as $ n) {if ($ this-> _ rule-> IsValid ($ n) {echo $ n, "\ n ";}}}} /*** Class Client */class Client {/*** test */public static function test () {$ invoker = new Invoker (new ConcreteCommand ()); $ invoker-> process (array (-1,-4,-8, 1, 10, 15, 20, 36, 48, 59,111); echo'
'; $ Invoker = new Invoker (new ConcreteCommandTwo (); $ invoker-> process (array (-1,-4,-8, 1, 10, 15, 20, 36, 48, 59,111) ;}// run the test Client: test ();

Running result:

1 10 15 20 36 48 59 111 -4 -8 10 20 36 48

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.