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