What is command mode?
From this very good understanding of life, the remote control TV is a typical command mode.
The concept of the command pattern: " behavior requestor " and " Behavior Realization Person " usually presents a kind of " tight coupling ". However, in some cases, such as "record, Undo/Redo, transaction" and so on, this can not resist the change of tight coupling is inappropriate. In this case, how do you decouple the "behavior requestor" from the "behavior-implementing Person"? abstract a set of behaviors into objects to achieve loose coupling between them. This is the command pattern---- When you have thoroughly parsed the command pattern, look back and understand this sentence.
Why use Command mode?
Understanding why to use Command mode is the key to learning the command pattern.
In-depth understanding of command patterns:
1. The essence of the command pattern is to encapsulate the command, separating the responsibility for issuing the order and the responsibility for executing the order .
2. Each command is an action: the requesting party makes a request, requests an action, the receiving party receives the request, and executes the action .
3. The command pattern allows the requesting party and the receiving party to be independent, 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.
4. The command pattern makes the request itself an object that can be stored and passed in the same way as other objects. -because this feature can be used to achieve the concept of "record, Undo/Redo, transaction" and other processing,
5. The key to the command pattern is the introduction of an abstract command interface , and the sender is programmed for an abstract command interface , and only specific commands that implement an abstract command interface can be associated with the receiver .
The above 5 points make a deeper analysis of the command pattern. It is very helpful to understand and design command patterns.
/*** Design a lamp class as the implementation of the Act.*/ Public classLight {//the position of the lampString loc= ""; PublicLight (String Loc) { This. loc=Loc; } Public voidOn () {System.out.println (loc+ "on"); } Public voidOff () {System.out.println (loc+ "Off"); }}
/***/Publicinterface Command {publicvoid execute (); Public void undo ();}
Each command corresponds to a class
/*** Turn on the light command * in-depth understanding of the 4th in command mode*/ Public classLightoffcommandImplementscommand{PrivateLight light ; PublicLightoffcommand (light) { This. light=Light ; } @Override Public voidExecute () {//TODO auto-generated Method StubLight . Off (); } @Override Public voidundo () {//TODO auto-generated Method Stub } }
/***/Publicinterface Control {publicvoid Onbutton (int slot); Public void Offbutton (int slot);}
/** * behavior requestor implementation, CONTROLLER implementation * This is also the key */ public class medocontrol implements Control { Span style= "COLOR: #0000ff" >private command[] Oncommands; private command[] Offcommands; public Medocontrol () {Oncommands = new Command[5]; Offcommands = new command[5];
//This is an empty command in which the Undo method and the Execute party do nothing. Command Nocommand = new Nocommand (); for (int i=0;i< Oncommands.length;i++) {Oncommands[i] =nocomman D Offcommands[i] =nocommand; }}//This is the initialization of this controller, that is, the remote control has those keys, these keys to set his settings to what command.
//slot represents that key, which corresponds to what command on each key public void setcommand (int Slot,command on , Command off) { Oncommands[slot]= on; Offcommands[slot]=off; } @Override publicvoid Onbutton (int slot) { Oncommands[slot]. Execute (); } @Override publicvoid Offbutton (int slot) { Oncommands[slot] . Execute ();} }
Java design mode 5-command mode