Design mode Command

xiaoxiao2021-03-06  176

?

Design mode Command

The people in the bridge http://www.jdon.com 2002/4/23 /

Click here to participate in the monthly design pattern lecture.

Command mode is the most confused model. After reading a lot of code, he feels vague to master it probably, I think it is important to understand the principle structure, so that you can actually program your actual programming. Command mode is actually not very specific, stipulating a lot of modes, is this flexibility, which is some confuse.

Command Definition N The request from the client is incoming an object without having to understand the action of this request or the details of the processes that accept this request.

This is a mode of communication contact nature between two machines, similar to the traditional process language Callback function.

Advantages: Decoupting between senders and recipients. The sender calls an operation, the recipient accepts the request to perform the corresponding action because the sender does not need to know any interface of the recipient using the Command mode.

The code of many Command mode is for the graphical interface. It is actually the menu command, when we select a command in a drop-down menu, then do some actions.

Packing these commands into a class, then the user (caller) operates on this class, this is the Command mode, in other words, the original user (caller) is directly called these commands, such as the menu open the document (The caller), directly point to the code to open the document, use the Command mode, which is increasing between the two, and the direct relationship is broken, and both are isolated, it is basically no relationship.

Obviously, the benefits of doing this are in line with the characteristics of the package, reduce the coupling, and Command is a typical mode that will encapsulate behavior. Factory is a mode that will be created, from the Command mode, I also found a "Tongxia": Good I like to complicate simple questions. I like to increase the third party in different classes, of course, doing the robustness that is conducive to the code is still reused.

How to use? Specific Command mode code is a variety of, because how to encapsulate commands, different systems, there are different practices. The following example is to encapsulate commands in a collection, and once any object is added to the list, actually load In a closed black box, the characteristics of the object disappeared, only when it is taken out, there is a possibility of blurring: a typical Command mode requires an interface. There is a unified method in the interface, this is "" will "request command / request Packaged object ":

Public interface command {public abstract void execute ();

Specific Different Commands / Request Codes are implementation interface Command, and there are three specific commands below

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}}

According to the usual practice, we can call these three Command directly, but use the Command mode, we have to pack them, throw it 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 orders entered the list, they have lost their appearance characteristics. After removing, they may not tell who is engineer who is programmer, see how the following client calls Command mode: public class testcommand {public static void main (String [] args) {list queue = product.ProduceRequests (); for (Iterator it = queue.iterator (); it.hasnext ();) // Client directly calls the Execute method, there is no need to know the other by the caller More class method names. ((Command) IT.NEXT ()). Execute ();}}

It can be seen that the caller is basically only dealing with the interface, which does not agree to interact, which also reflects a principle that programming, so that when the fourth specific command is added later, it is not necessary to modify the code in the caller TestCommand. Understand the core principle of the above code, in use, you should have its own method, especially how to separate the caller and specific commands, there are many implementation methods, the above code is "from list" The practice is just to demonstrate this approach.

A good reason to use the Command mode is also because it can implement the UNDO function. Each specific command can remember the action it just execute and recovered when needed.

Command mode Application in interface design. Java's swing menu command is used using Command mode. Since Java is still lacking in the performance of interface design, we will not discuss the interface design specific code, there are many examples on the Internet. .

Reference: http://www.patternDepot.com/put/8/command.pdf

http://www.javaworld.com/javaworld/javatips/jw-javatip68.html

How to design a design model See "Java Practical System Development Guide" in specific projects

转载请注明原文地址:https://www.9cbs.com/read-128741.html

New Post(0)