[Design mode-behavior type] Command mode)

Source: Internet
Author: User

[Design mode-behavior type] Command mode)
One sentence

The request and execution of commands are represented by different objects. (The advantage is that it can be developed independently or abolished)

 

 

Summary

 

 

 

Analysis

COMMAND-I have a MM family with very strict management and can't meet each other. I had to use her brother to send messages between us. What instructions does she give me, write a piece of paper for her brother to bring to me. No, her brother sent another COMMAND. To thank him, I asked him to eat a bowl of soy sauce. He said, "I also sent COMMAND to my sister and three boyfriends, you are the least angry to ask me to eat noodles."
Command mode: The command mode encapsulates a request or operation into an object. The command mode separates the responsibility for issuing commands from the responsibility for executing commands and delegates them to different objects. In command mode, the requester and the sender are allowed to be independent, so that the requester does not have to know the interfaces of the receiver, how the request is received, and whether the operation is executed, when and how it is executed. Command revocation supported by the system

 

Instance

The instance includes:

Define a class for executing commands (javaser. java)

Define a Command interface (Command. java)

Inherit from the interface above to define an implementation class (CommandImpl. java)

Define a class that calls commands (Invoker. java)

Test (TestMain. java)

 

/**    * @author oscar999    * @date 2015-1-6 * @version V1.0    */package designptn.command;public class Receiver {public void reveive() {System.out.println("Receiver do Somethings!");}}

/**    * @author oscar999    * @date 2015-1-6 * @version V1.0    */package designptn.command;public abstract class Command {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 {private 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        Receiver rec = new Receiver();        Command cmd = new CommandImpl(rec);        Invoker i = new Invoker();        i.setCommand(cmd);        i.execute();    }}


 

 

 

 

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.