PHP design mode command pattern using example _php instance

Source: Internet
Author: User
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 ();
?>

  • 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.