Statement: This series of blog reference "Big Talk design mode", author Geoscience.
Command mode: in a software system, the "behavior requestor" and "The behavior realization person" usually present a kind of "tight coupling". However, in some cases, such as "record, undo / Redo, transaction" and so on, this can not resist the change of tight coupling is inappropriate. In this case, how do you decouple the "behavior requestor" from the "behavior-implementing Person"? Abstract a set of behaviors into objects to achieve loose coupling between them. This is the command pattern.
Class Diagram:
Role Analysis:
Abstract command : defines the interface of the command, declaring the method of execution.
Specific Commands : Command interface Implementation object, is the implementation of "virtual"; Typically, the receiver is held and the function of the receiver is called to complete the operation to be performed by the command.
Command Recipient : Recipient , the object that actually executes the command. Any class can become a recipient, as long as it can implement the appropriate functionality that the command requires.
Controlling Person : Requirements The command object executes the request, usually holding the command object, and can hold many command objects. This is where the client actually triggers the command and asks the command to perform the appropriate action, which is equivalent to using the Command object's entry.
Core code:
<?php/** * Created by Phpstorm. * user:lyl * DATE:2015/6/4 * time:20:21 *//** * TV is the recipient of the request, * There are some buttons on the remote control, different buttons correspond to different operation of the TV. * Abstract command roles are played by a command interface, with three specific command classes implementing an abstract command interface, * These three specific command classes represent three operations: turn on the TV, turn off the TV, and switch channels. * Obviously, the TV remote is a typical application example of command mode. *//** Command Recipient * Class Tv */class tv{public $curr _channel=0; /** * Turn on the TV */Public Function turnOn () {echo "The television are on.". " <br/> "; /** * Turn off the TV */Public Function turnoff () {echo "the television is off.") <br/> "; }/** Switch Channel * @param $channel Channel */Public Function Turnchannel ($channel) {$this->curr_channe L= $channel; echo "This TV Channel is". $this->curr_channel. " <br/> "; }}/** Execute 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 (); }}
Calling client code:
Header ("Content-type:text/html;charset=utf-8");//----------------------Command mode--------------------require_once ". Command/command.php ";//test code//Command Receiver $myTv = new Tv ()///boot command $on = new Commandon ($myTv);//shutdown Command $off = new Commandoff ($ MYTV);//channel switch command $channel = new Commandchannel ($myTv, 2);//Command Control object $control = new Control ($on, $off, $channel);//Boot $con Trol->turnon ();//Switch Channel $control->changechannel ();//Shutdown $control->turnoff ();
Applicable scenarios:
1. The system needs to decouple the requesting caller from the request receiver so that the caller and receiver do not interact directly.
2. the system needs to specify requests at different times, queue requests, and execute requests.
3. The system needs to support the undo (undo) operation and Recovery (Redo) operation of the command .
4. the system needs to combine a set of operations, that is, macro commands are supported.
Advantages
1. Reduce the coupling between the objects.
2. New commands can be easily added to the system.
3. It is easier to design a combination command.
4. Call the same method to implement different functions
Disadvantages
Using command mode may cause some systems to have too many specific command classes. Because you need to design a specific command class for each command, some systems may require a large number of specific command classes, which will affect the use of the command pattern.
Welcome to follow my video course, the address is as follows, thank you.
PHP Object-oriented design pattern
PHP design mode-command mode