Design mode Command

zhaozj2021-02-16  96

Design mode Command

Command mode is the most confused model. After reading a lot of code, I feel that I can't understand the principles, I think it is important to understand the principle structure, so that I have a guiding role in my actual programming. . Command mode is actually not very specific, stipulating a lot of modes, is this flexibility, which is some confuse.

Command defines a lot of COMMAND mode, which is for graphical interfaces. It is actually a functional table command, and we will perform some actions when you select a command in a drop-down feature.

Packing these commands into a category, then the user (caller) is operated again, which is a Command mode. In other words, the user (caller) is directly calling these commands, such as the function table Document (caller), directly point to the code of the document, use the Command mode, adding a middle person between the two, breaking this direct relationship, and both are isolated, there 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 categories, 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, have different practices. The following example is to encapsulate the command in a Collection's list. Once any object is added to the list, in fact, in a closed black box, the characteristics of the object disappear, only when it is removed, it is possible to distinguish:

Typical Command mode needs to have a interface. There is a unified method in the interface, which is "encapsulates the commands":

Public interface command {public abstract void execute ();

Different commands / request code are real interface Command, 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, and they will be removed later, and they may not distinguish who is engineer who is Programmer, look at how to call Command mode:

Public class testCommand {public static void main (string [] args) {list queue = product.ProduceRequests (); for (item it = queue.ITerator (); it.hasnext ();) // Remove the list of List East, other Features cannot be determined, only one feature is 100% correct, // They are at least the "son" of the intermand. So forced conversion categories as intermand ((Command) it.next ()). EXECUTE ();}} It can be seen that the caller is basically retrieved, and it is not interactive, which also reflects a principle, facing Interface 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 implementations, the above code is "from list" way of doing. This approach is just to demonstrate.

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

Command mode is widely used in interface design. Java's Swing function list commands are used using the Command mode. Since Java is still lacking in the performance of the interface design, we do not discuss the specific code of the interface design, there are many examples on the Internet.

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

New Post(0)