[Mode overview] ---------- by xingoo
Pattern intent
Encapsulate a request into an object and execute undo, redo, and other operations on the command.
In typical eclipse development, Editor operations require this mode, such as Undo and redo.
In addition, this mode decouples the trigger and receipt of a command, so that we can evolve into receiving the command for an object of interest. When the command is triggered, these objects will execute the operation. This mechanism is also used to handle Java events.
Application scenarios
1. The command is abstracted as an object.
2. Specify or queue commands at different times
3. Supports undo, redo, and other operations.
4. Modify the log. When the system crashes, cancel the modification log.
5. Construct a high-level system in primitive operations (not understood)
Mode Structure
InvokerTo trigger the execution of a command.
/*** Command trigger, send command * @ author xingoo **/class invoker {private commond; Public invoker (commond) {This. commond = commond;} public void action () {commond. excute ();}}
CyclerThe receiver of the command.
/*** The receiver of the command, responsible for receiving the command and processing * @ author xingoo **/class aggreger {public aggreger () {} public void action () {system. out. println ("action of role er! ");}}
CommondCommand abstract interface
/*** Command interface, which defines the unified command interface * @ author xingoo **/interface commond {public void excute ();}
ConcretecommondA specific command is associated with a receiver object. When the command is executed, the operation corresponding to the receiver is executed.
/*** Specific command * @ author xingoo **/class concretecommond implements commond {private extends er; Public concretecommond (extends er extends ER) {This. extends ER = extends er;} public void excute () {extends er. action ();}}
All code
1 package COM. xingoo. commond; 2/** 3 * command trigger, send command 4 * @ author xingoo 5*6 */7 class invoker {8 private commond; 9 10 public invoker (commond) {11 this. commond = commond; 12} 13 14 public void action () {15 commond. the recipient of the excute (); 16} 17} 18/*** 19 * command, responsible for receiving the command, process 20 * @ author xingoo21 * 22 */23 class Uploader {24 25 public Referer () {26 27} 28 29 public void action () {30 system. out. printl N ("action of role er! "); 31} 32} 33/** 34 * command interface, define the unified interface 35 * @ author xingoo36 * 37 */38 interface commond {39 public void excute (); 40} 41/** 42 * specific command 43 * @ author xingoo44 * 45 */46 class concretecommond implements commond {47 48 private extends er; 49 50 public concretecommond (extends er extends ER) {51 this. extends ER = extends er; 52} 53 54 public void excute () {55 extends er. action (); 56} 57 58} 59/** 60 * client caller 61 * @ auth Or xingoo62 * 63 */64 public class client {65 public static void main (string [] ARGs) {66 receiver er extends ER = new receiver Er (); 67 commond = new concretecommond (comment ER); 68 system. out. println ("commond register in here! "); 69 70 try {71 thread. sleep (3000); 72} catch (interruptedexception e) {73 // todo auto-generated catch block74 E. printstacktrace (); 75} 76 77 system. out. println ("commond excute in here! "); 78 invoker = new invoker (commond); 79 invoker. Action (); 80} 81}
View code
Running result
Commond register in here!Commond excute in here!Action of receiver!
[Design mode] -- command mode commond