Design Mode Command
Banqiao Renren http://www.jdon.com 2002/4/23/
Click here to attend monthly design model lectures
The command mode is the most confusing mode. I have read a lotCodeAfter that, I felt vaguely aware of its general principles. I think the most important thing to understand the design pattern is to grasp the principle structure, so as to provide guidance for actual programming. the command mode is actually not very specific. It defines many modes. It is precisely this flexibility that makes people have some confuse.
Command Definition
N incoming requests from the client to an object. You do not need to understand the request activation action or the handling details about the request.
This is a communication mode between two machines, similar to the callback function of the traditional process language.
Advantages:
Decouples the contact between the sender and the receiver. The sender calls an operation and the receiver accepts the request to execute the corresponding action. Because the command mode is used for decoupling, the sender does not need to know the receiver's interface.
Many Command-mode codes are for graphic interfaces. They are actually menu commands. When we select a command from a drop-down menu, some actions will be executed.
Encapsulate these commands into a class, and then the user (caller) operates on the class. This is the command mode. In other words, the original user (caller) is to directly call these commands, such as opening the document (caller) on the menu, direct to the code to open the document, using the command mode, is to add an intermediary between the two, disconnects the direct link and isolates the two.
Obviously, the advantage of doing so is to conform to the encapsulation characteristics and reduce the coupling degree. Command is a typical mode for encapsulating behavior, and factory is a mode for encapsulating creation,
From the command mode, I also found that the design mode is a "common problem": it seems that I like to complicate simple problems, and I like to add third parties in different classes, of course, this is conducive to code robustness, maintainability, and reusability.
How to use it?
There are various types of specific command mode code, because different systems have different methods to encapsulate commands. in the following example, commands are encapsulated in a collection list. Once any object is added to the List, a closed black box is actually loaded, and the Object Features disappear, it is possible to blur the Resolution:
A typical command mode requires an interface. The interface has a uniform method, which is "encapsulating commands/requests as objects ":
Public interface command { Public abstract void execute (); } |
The specific command/Request Code is the implementation interface command. The following three specific commands
| Public class engineer implements command { Public void execute (){ // Do engineer's command } } Public class programmer implements command { Public void execute (){ // Do programmer's command } } Public class politician implements command { Public void execute (){ // Do politician's command } } |
Normally, we can directly call these three commands, but in the command mode, we need to encapsulate them and drop them into the black box list:
Public class producer { Public static list producerequests (){ List queue = new arraylist (); Queue. Add (New domesticengineer ()); Queue. Add (New politician ()); Queue. Add (New Programmer ()); Return queue; } } |
After these three commands enter the list, they have lost their external features and will be retrieved later. They may not be able to tell who is the engineer and who is the programmer, see how the following client calls the command mode:
Public class testcommand { Public static void main (string [] ARGs ){
List queue = producer. producerequests (); For (iterator it = queue. iterator (); it. hasnext ();)
// The client directly calls the execute method, so you do not need to know more method names of the caller. (Commandit.next(cmd.exe cute ();
} } |
It can be seen that the caller basically only deals with interfaces and does not implement interaction in a specific way. This also reflects the principle of interface-oriented programming. In this way, when the fourth specific command is added in the future, you do not have to modify the code in the caller testcommand.
After understanding the core principles of the above Code, you should have your own methods in use. In particular, there are many implementation methods for how to separate callers and specific commands, the above Code uses the "from list once" method. this is just for demonstration.
A good reason for using the command mode is that it can implement the Undo function. Every specific command can remember the action it just executed and restore it as needed.
Command mode is widely used in interface design. in Java swing, menu commands use the command mode. Because Java lacks the performance of the interface design, we will not discuss the specific code of the interface design. There are many such examples on the network.
Refer:
Http://www.patterndepot.com/put/8/command.pdf
Http://www.javaworld.com/javaworld/javatips/jw-javatip68.html