sentence
The execution of a command's request and command is represented by a different object. (Benefits can be independently developed, can also be revoked)
Summarize
parsing
command-I have a mm home tube special strict, can't meet, had to help her brother in between us to send information, she told me what instructions, write a note let her brother bring me. This is not, her brother sent over a command, in order to thank him, I asked him to eat a bowl of mixed sauce noodles, how he said: "I also give my sister three boyfriend sent command, count you the most stingy, just ask me to eat noodles." ”
Command mode: The command mode encapsulates a request or action into an object. The command mode divides the responsibility for issuing the command and the responsibility for executing the command, delegating it to different objects. The command pattern allows the requesting party to be independent of the sending party, so that the requesting party does not have to know the interface of the party receiving the request, not to know how the request was received, and whether the operation was executed, when it was executed, and how it was executed. System support for Undo of commands
Example
Examples include:
Defines a class that executes a command (Receiver.java)
Defines an interface for a command (Command.java)
Inherit the above interface to define an implementation class (Commandimpl.java)
Defines a class that invokes a command (Invoker.java)
Test (Testmain.java)
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.command;public class Receiver {PU Blic void Reveive () {System.out.println ("Receiver do somethings!");}}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.command;public abstract class Com Mand {protected receiver receiver;public Command (receiver receiver) {this.receiver = receiver;} public abstract void execute ();}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.command;public class Commandimpl Extends Command {public Commandimpl (receiver receiver) {super (receiver);} public void Execute () {receiver.reveive ();}}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.command;public class Invoker {pri vate command command;public void setcommand (Command command) {this.command = command;} public void Execute () {Command.Execute ();}}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.command;public class Testmain {
/** * @param args * /public static void main (string[] args) { //TODO auto-generated method stub R Eceiver rec = new Receiver (); Command cmd = new Commandimpl (rec); Invoker i = new Invoker (); I.setcommand (cmd); I.execute (); }}
[design mode-behavioral] Command mode