PHP design mode-command mode

Source: Internet
Author: User

PHP design mode-command mode

 

Command mode: In software systems, "behavior requestor" and "behavior implementer" usually present a "tightly coupled ". However, in some cases, such as "record, undo/Redo, and transaction" operations, it is not suitable for tight coupling that cannot withstand changes. In this case, how can we decouple "behavior requestor" from "behavior implementer? Abstract A group of behaviors as objects to achieve loose coupling between them. This is the command mode.

 

Class diagram:

Role analysis:

Abstract command: defines the command interface and declares the execution method.

Specific commands: The Implementation object of the command interface is a "virtual" implementation. Generally, it holds the receiver and calls the receiver's function to complete the operations to be executed by the command.

Command Recipient: The recipient, the object that actually executes the command. Any class may become a receiver, as long as it can implement the corresponding functions required by the command.

Controller: requires the command object to execute the request. Generally, it holds the command object and can hold many command objects. This is the place where the client actually triggers the command and requires the command to execute the corresponding operation, that is, it is equivalent to the entry of the command object.

 

Core code:

 

 ;}/*** Turn off The TV */public function turnOff () {echo The television is off ..;} /** switch channel * @ param $ channel */public function turnChannel ($ channel) {$ this-> curr_channel = $ Channel; echo This TV channel is. $ this-> curr_channel .;}} /** execution command Interface * interface ICommand */Interface ICommand {function execute ();}/** boot command * Class CommandOn */class CommandOn implements ICommand {private $ TV; public function _ construct ($ TV) {$ this-> TV = $ TV;} public function execute () {$ this-> TV-> turnOn ();}} /** shutdown command * Class CommandOn */class CommandOff implements ICommand {private $ TV; public function _ construct ($ TV) {$ this-> TV = $ TV ;} public function execute () {$ this-> TV-> turnOff () ;}/ ** switch channel command * Class CommandOn */class CommandChannel implements ICommand {private $ TV; private $ channel; public function _ construct ($ TV, $ channel) {$ this-> TV = $ TV; $ this-> channel = $ channel ;} public function execute () {$ this-> TV-> turnChannel ($ this-> channel );}} /** remote Control * Class Control */class Control {private $ _ onCommand; private $ _ offCommand; private $ _ changeChannel; public function _ construct ($ on, $ off, $ channel) {$ this-> _ onCommand = $ on; $ this-> _ offCommand = $ off; $ this-> _ changeChannel = $ channel;} public function turnOn () {$ this-> _ onCommand-> execute ();} public function turnOff () {$ this-> _ offCommand-> execute ();} public function changeChannel () {$ this-> _ changeChannel-> execute ();}}

 

Call the client code:

 

Header (Content-Type: text/html; charset = UTF-8); // ---------------------- command mode -------------------- require_once. /Command. php; // test code // command receiver $ myTv = new TV (); // boot command $ on = new CommandOn ($ myTv ); // shutdown command $ off = new CommandOff ($ myTv); // channel switching command $ channel = new CommandChannel ($ myTv, 2 ); // command control object $ Control = new control ($ on, $ off, $ channel); // start $ control-> turnOn (); // switch the channel $ control-> changeChannel (); // shut down $ control-> turnOff ();

 

Applicable scenarios:

 

1. The system needs to decouple the request caller from the request receiver so that the caller and the receiver do not directly interact.

2. The system needs to specify the request, queue the request, and execute the request at different times.

3. The system must support Undo and Redo operations of commands.

4. The system must combine a group of operations to support macro commands.

Advantages

1. Reduce the coupling between objects.

2. New commands can be easily added to the system.

3. You can easily design a combined command.

4. Call the same method to implement different functions

 

 

 

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.