[design mode-behavioral] Command mode

Source: Internet
Author: User

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

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.