Php design mode command mode example

Source: Internet
Author: User

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 ();

?>

 

Related Article

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.