[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(); }}