Command mode is the most let me doubt a pattern, I read a lot of code, just feel vaguely grasp its general principle, I think understanding design mode is the most important to grasp the principle of construction, so that the actual programming of their own guidance. Command mode is actually not a very specific, a lot of patterns, it is this flexibility, let a person some confuse.
Command definition
Many command mode code is for the graphical interface, it is actually a menu command, we select a command in a Drop-down menu, and then do some action.
Encapsulate these commands in a class, the user (caller) then operates on the class, which is the command mode, in other words, the user (caller) directly invokes the commands, such as the Open Document (caller) on the menu, directly to the code that opens the document, using command mode, is to add an intermediary between the two, and this direct relationship is broken, while the two are isolated, basically no relationship.
Obviously the advantage of doing this is that it conforms to the encapsulated features and reduces the coupling, and the command is the typical pattern that encapsulates the behavior, factory is the pattern that will be created for encapsulation.
From command mode, I've also found that design patterns are a "common problem": it's as if you like to complicate simple problems, like adding third parties to different classes, and of course it's good for the robustness of the code and reusability.
How to use Command mode
The specific command pattern code is various, because how to encapsulate commands, different systems, have different practices. The following example encapsulates a command in a collection list, and any object once added to the list is actually loaded into a closed black box, and the object's properties disappear, and only when it is removed can it be blurred.
A typical command pattern requires an interface. There is a unified approach in the interface, which is "encapsulate a command/request as an object":
Copy Code code as follows:
Public interface Command {
public abstract void execute ();
}
The specific command/request code is the implementation interface command, with the following three specific commands:
Copy Code code as follows:
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
}
}
As usual, we can call the three command directly, but using command mode, we'll encapsulate them and throw them into the black box list:
Copy Code code as follows:
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 entering the list, these three commands have lost their appearance, and may not be able to tell who is engineer who is programmer, and see how command mode is invoked below:
Copy Code code as follows:
public class Testcommand {
public static void Main (string[] args) {
List queue = Producer.producerequests ();
for (Iterator it = Queue.iterator (); It.hasnext ();)
Take out the list of Dongdong, other features are not sure, only to ensure that one feature is 100% correct,
They are at least the "son" of the interface command. So the cast type is the interface command
(Command) It.next (). Execute ();
}
}
Thus, the caller basically only interfaces with the interface, not specific implementation of the interaction, which also embodies a principle, interface programming, so that later add the fourth specific command, you do not have to modify the caller Testcommand code.
Understanding the core principles of the above code, in use, should each have their own methods, especially in how to separate callers and specific commands, there are many implementation methods, the above code is to use the "from the list over" practice. This practice is only for demonstration purposes.
A good reason to use command mode is also because it implements the Undo function, and each specific command remembers the action it just performed and recovers when needed.
Command mode is widely used in interface design. In the Java Swing menu commands are used in command mode, because Java in the interface design performance is still deficient, so the interface design specific code we do not discuss, there are many such examples on the network.