Command class:
1. Command role: declares an abstract interface to all specific command classes. This is an abstract role.
2. Specific command role: Define a weak coupling between the recipient and the behavior, and implement the Execute method, which is responsible for invoking the appropriate actions that are accepted. The Execute () method is often called the execution method
3. Customer role: Create a specific Command object and determine its recipient.
4. Requestor role: Responsible for invoking the Command object execution request, the related method is called the action method.
5. Recipient role: Responsible for specific implementation and execution of a request.
Role:
1. Abstract the action to be executed to parameterize the object.
2. Specify, arrange, and execute requests at different times.
3. Support Cancel operation
4. Support Modification Log
Copy CodeThe code is as follows:
Command interface
Interface command{
Public function execute ();
}
Specific commands
Class Concretecommand implements command{
Private $_receiver;
Public function __construct ($receiver) {
$this->_receiver = $receiver;
}
Public Function execute () {
$this->_receiver->action ();
}
}
Accepted by
Class receiver{
Private $_name;
Public function __construct ($name) {
$this->_name = $name;
}
Methods of Action
Public Function action () {
echo $this->_name ' do action.
';
}
}
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 () {
$receiver = new Receiver (' Jaky ');
$command = new Concreteecommand ($receiver);
$invoker = new Invoker ($command);
$invoker->action ();
}
}
Client::main ();
?>