Php design mode command mode example
This article describes how to use the command mode in php design mode. The command mode encapsulates a request as an object so that you can parameterize the customer with different requests; queue requests or record request logs, and supports unrecoverable operations
Command class:
1. Command role: declares an abstract interface for all specific command classes. This is an abstract role.
2. Specific command role: defines the weak coupling between a receiver and behavior; implements the execute method and is responsible for calling the corresponding operations accepted. The execute () method is usually called the execution method.
3. Customer role: create a specific command object and determine its receiver.
4. requester role: the requester role is responsible for calling command objects to execute requests. The related methods are called Action methods.
5. Recipient role: responsible for implementing and executing a request.
Purpose:
1. abstract the action to be executed to parameterize the object.
2. Specify, sort, and execute requests at different times.
3. cancelling is supported.
4. Logs can be modified.
Copy the Code as follows:
<? Php
// Command INTERFACE
Interface Command {
Public function execute ();
}
// Specific command
Class ConcreteCommand implements Command {
Private $ _ Explorer;
Public function _ construct ($ javaser ){
$ This-> _ Cycler = $ Cycler;
}
Public function execute (){
$ This-> _ er-> action ();
}
}
// Recipient
Class extends er {
Private $ _ name;
Public function _ construct ($ name ){
$ This-> _ name = $ name;
}
// Action Method
Public function action (){
Echo $ this-> _ name. 'Do action. <br/> ';
}
}
// Requester
Class Invoker {
Private $ _ command;
Public function _ construct ($ command ){
$ This-> _ command = $ command;
}
Public function action (){
$ This-> _ command-> execute ();
}
}
// Client
Class Client {
Public static function main (){
$ Cycler = new Cycler ('jaky ');
$ Command = new ConcreteeCommand ($ receiver );
$ Invoker = new Invoker ($ command );
$ Invoker-> action ();
}
}
Client: main ();
?>