Learning php design mode php command mode)

Source: Internet
Author: User
Tags php example
This article mainly introduces the command mode in the php design mode and uses php to implement the command mode. if you are interested, refer I. intention
Encapsulate a request as an object to parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations.
The variable aspect is: when and how to satisfy a request
Command mode is the encapsulation of commands. The command mode separates the responsibility for issuing commands from the responsibility for executing commands and delegates them to different objects.
The requesting party sends a request to perform an operation. the receiving party receives the request and performs the operation. The command mode allows the requester and the receiver to be independent, so that the requester does not have to know the interfaces of the receiver, or how the request is received, and whether the operation is executed, when it is executed, and how it is executed.
II. command mode structure

III. main roles in command mode
Command role: declares an abstract interface for all specific Command classes. This is an abstract role.
ConcreteCommand role: defines the weak coupling between a receiver and behavior, implements the Execute () method, and is responsible for calling the corresponding operations of the acceptance test. The Execute () method is usually called the execution method.
Client role: Creates a ConcreteCommand object and determines its receiver.
The Invoker role is used to call a command object to execute a request. the related method is called the action method.
Receiver role: Implements and executes a request. Any class can be the receiver. the method for implementing and executing requests is called the action method.
IV. Advantages of command mode
Advantages of command mode:
1. the command mode separates the object requesting an operation from the object that knows how to execute an operation.
2. the command class can be modified and promoted like any other class.
3. the command objects can be aggregated and merged into a synthesis command.
4. you can easily add new command classes.
Disadvantages of the command mode: Some systems may have too many specific command classes.
V. applicable scenarios of the command mode
1. abstract the action to be executed to parameterize the object. The Command mode is an object-oriented alternative to the callback mechanism.
2. specify, arrange, and execute requests at different times.
3. cancel the operation.
4. logs can be modified.
5. construct a system using high-level operations built on primitive operations. The Command mode provides a method to model transactions. Command has a public interface that allows you to call all transactions in the same way. At the same time, it is easy to add new transactions to expand the system.
VI. command mode and other modes
Merging mode (composite mode): the Composite mode can be used to implement macro commands.
Prototype mode: If the command class carries the clone (or copy method mentioned in the previous article) method, the command can be copied. This mode may be used when the Command mode supports multiple cancellation operations to copy the Command object in the current state.
VII. command mode PHP example

<? Php/*** Command role */interface Command {/*** execution method */public function execute ();} /*** specific Command role */class ConcreteCommand implements Command {private $ _ aggreger; public function _ construct (aggreger $ aggreger) {$ this-> _ cycler = $ cycler;}/*** execution method */public function execute () {$ this-> _ Cycler-> action ();}} /*** Receiver role */class Receiver {/* Receiver name */private $ _ name; public function _ construct ($ name) {$ this-> _ name = $ name;}/*** action method */public function action () {echo $ this-> _ name, 'Do action.
';}}/*** Requester role */class Invoker {private $ _ command; public function _ construct (Command $ command) {$ this-> _ command = $ command;} public function action () {$ this-> _ command-> execute ();}} /*** Client */class Client {/*** Main program. */public static function main () {$ aggreger = new aggreger ('phppp'); $ command = new ConcreteCommand ($ aggreger); $ invoker = new Invoker ($ command ); $ invoker-> acti On () ;}} Client: main () ;?>


8. command mode collaboration
1. the Client creates a ConcreteCommand object and specifies its Receiver object.
2. an Invoker object stores the ConcreteCommand object
3. the Invoker submits a request by calling the execute operation of the Command object. If the command is unrecoverable, ConcreteCommand stores the current status before executing the execute operation to cancel the command.
4. the ConcreteCommand object performs operations on the consumer that calls it to execute the request.
9. Macro commands
Here, we use a simple add and paste function as an example to combine these two commands into a Macro command.
We can create a new copy command and paste command, and then add it to the macro command.
The code is as follows:

<? Php/*** Command role */interface Command {/*** execution method */public function execute ();} /*** Macro Command interface */interface MacroCommand extends Command {/*** Macro Command aggregation management method, you can delete a member Command * @ param command $ Command */public function remove (command $ Command);/*** Macro command aggregation management method, you can add a member Command * @ param command $ Command */public function add (command $ Command);} class DemoMacroCommand implements MacroCommand {private $ _ commandList; public function _ construct () {$ this-> _ commandList = array ();} public function remove (Command $ command) {$ key = array_search ($ command, $ this-> _ commandList ); if ($ key = FALSE) {return FALSE;} unset ($ this-> _ commandList [$ key]); return TRUE;} public function add (Command $ command) {return array_push ($ this-> _ commandList, $ command);} public function execute () {foreach ($ this-> _ commandList as $ command) {$ command-> execute () ;}}/ *** copy Command role */class CopyCommand implements command {private $ _ policer; public function _ construct (Receiver $ receiver) {$ this-> _ receiver = $ receiver;}/*** execution method */public function execute () {$ this-> _ er-> copy () ;}/ *** copy Command role */class PasteCommand implements Command {private $ _ receiver; public function _ construct (Receiver $ receiver) {$ this-> _ receiver = $ receiver;}/*** execution method */public function execute () {$ this-> _ receiver er-> paste () ;}/ *** Receiver role */class receiver {/* Receiver name */private $ _ name; public function _ construct ($ name) {$ this-> _ name = $ name;}/*** copy method */public function copy () {echo $ this-> _ name, 'Do copy action.
';}/*** Paste method */public function paste () {echo $ this-> _ name, 'Do paste action.
';}}/*** Requester role */class Invoker {private $ _ command; public function _ construct (Command $ command) {$ this-> _ command = $ command;} public function action () {$ this-> _ command-> execute ();}} /*** Client */class Client {/*** Main program. */public static function main () {$ Pipeline er = new pipeline er ('phpppan '); $ pasteCommand = new PasteCommand ($ Pipeline er); $ copyCommand = new CopyCommand ($ Pipeline er ); $ ma CroCommand = new DemoMacroCommand (); $ macroCommand-> add ($ copyCommand); $ macroCommand-> add ($ pasteCommand); $ invoker = new Invoker ($ macroCommand ); $ invoker-> action (); $ macroCommand-> remove ($ copyCommand); $ invoker = new Invoker ($ macroCommand); $ invoker-> action () ;}} Client:: main ();?>

The above is the code that uses php to implement the command mode. there are some concepts about the command mode, and I hope it will be helpful for you to learn.

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.