Defined
Passes a request from the client to an object, allowing you to parameterize the customer with different requests. For the "behavior requestor" and "The behavior realization" decoupling , can achieve the loose coupling between the two, in order to adapt to the change. Separation of changes and invariant factors.
UML diagram:
Role
Command: Defines the interface of the commands, declaring the method of execution.
Concretecommand: Command Interface Implementation object, is the implementation of "virtual", usually hold the receiver, and invoke the function of the receiver to complete the operation of the command to do.
Receiver: The receiver, 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.
Invoker: Requires command objects to execute requests, usually holding command objects, 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.
Client: Creates a specific command object and sets the recipient of the Command object. Note that this is not our regular client, but the assembly of the Command object and the receiver, and perhaps it would be better to refer to this client as the assembler, since the client that actually uses the command is triggering execution from Invoker.
The total order of calls is:
The requestor holds the Command object, invokes the Command object's method, and the Command object holds the recipient, which in turn invokes the execution method of the recipient.
Where applicable
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.
Simple Command mode
Package com.hust.command1;//Recipient class public class Receiver {//method for really executing command logic public Void action () { System.out.println ("Perform the specific action! "); }}
/** * */package com.hust.command1;/** * Abstract Command interface * */public interface Command { //execute specific operation of the commands void execute ();}
Package com.hust.command1;//the specific command class public class Concretecommand implements command { private Receiver receiver;//receiver as member variable public concretecommand (receiver receiver) { this.receiver=receiver; } Call the recipient's related method to execute the concrete logical public void execute () {receiver.action ();}}
/** * */package com.hust.command1;/** * Requestor class * */public class Invoker { private command command;//Command object make member variable p ublic Invoker (Command command) { this.command=command; } public void Action () { //Call the Command object related method, execute the specific command Command.Execute ();} }
Package Com.hust.command1;public class Client {/** * @param args */public static void main (string[] args) {//Constructs a recipient object rece Iver receiver=new Receiver ();//Constructs a Command object Command=new concretecommand (receiver);//Constructs a requestor object to request the Command object, The command object is going to perform operation Invoker invoker=new invoker (command); Invoker.action ();}}
Tetris game with 4 buttons, two left and right move buttons, a quick drop button, a morph button
A player who plays a game is the equivalent of a client, and the 4 buttons on the game are the same as the requestor, and the logical way to execute a specific button command can be considered a command role.
/** * */package com.hust.shutdown;/** * @author Administrator * */public class Tetrismachine {//Tetris//Real processing "left" The logical code of the operation public void ToLeft () {System.out.println ("left");} Logical code that really handles the "right" operation public void ToRight () {System.out.println ("right");} Logical code that really handles the "fast Fall" operation public void Fasttobuttom () { System.out.println ("Fast Fall"); } Logical code that really handles the change shape operation public void transform () { System.out.println ("Change Shape");} }
/** * */package com.hust.shutdown;/** * Abstract Command interface * */public interface Command { //execute specific operation of the commands void execute ();}
Package Com.hust.shutdown;public class Leftcommand implements Command {//receiver Tetris private tetrismachine machine; Public Leftcommand (Tetrismachine machine) {super (); this.machine = machine;} public void Execute () {//Call game console specific method to perform Operation Machine.toleft ();}}
Package Com.hust.shutdown;public class Rightcommand implements Command {//receiver Tetris private Tetrismachine machine;< C3/>public Rightcommand (Tetrismachine machine) {super (); this.machine = machine;} public void Execute () {//Call game console specific method to perform Operation Machine.toright ();}}
Package Com.hust.shutdown;public class Fallcommand implements Command {//receiver Tetris private tetrismachine machine; Public Fallcommand (Tetrismachine machine) {super (); this.machine = machine;} public void Execute () {//Call game console specific method to perform Operation Machine.fasttobuttom ();}}
Java design mode-command mode commands