The Command mode is the most confusing pattern. After reading a lot of code, I felt that I was vaguely mastering its general principles. I think that understanding the design pattern is mainly to master the principle structure, so that it can be a guiding role in your actual programming. The Command mode is actually not a very specific model, and it is precisely this flexibility that makes people feel a little confused.
Command definition
Many Command mode codes are targeted at the graphical interface. They are actually menu commands. When we select a command in a drop-down menu, we will then perform some actions.
Encapsulate these commands into a class, and then the user (the caller) operates on this class. This is the Command mode. In other words, the user (the caller) originally called these commands directly, such as opening a document on the menu. (Caller), directly points to the code that opens the document, and use the Command mode, which means adding an intermediate between the two, breaking this direct relationship, and isolating the two, basically no relationship.
Obviously the benefit of doing this is that it conforms to the characteristics of the packaging and reduces the coupling. Command is a typical pattern that will encapsulate behavior, and Factory is a pattern that will create encapsulation.
From the Command mode, I also found that a "common problem" of design mode: I seem to like to complicate simple problems, and like to add third parties to different classes. Of course, doing so is conducive to the robustness, maintainability and reuse of the code. sex.
How to use command mode
The specific Command mode codes are various, because there are different ways to encapsulate commands, different systems. The following example is to encapsulate a command in a Collection List. Once any object is added to the List, it is actually loaded into a closed black box. The object's characteristics disappear. Only when it is taken out can it be vaguely distinguished.
A typical Command mode requires an interface. There is a unified method in the interface, which is "encapsulate commands/requests into objects":
The code copy is as follows:
public interface Command {
public abstract void execute ( );
}
The specific command/request codes are to implement the interface Command, and there are three specific commands below:
The code copy is 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 these three Commands directly, but using the Command mode, we have to encapsulate them and throw them into the black box list:
The code copy is 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 these three commands enter the List, they have lost their appearance characteristics. If they are taken out later, they may not be able to tell who is the Engineer and who is the Programmer. See how to call the Command mode below:
The code copy is 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's middle and other features, and you can't determine it. You can only ensure that one feature is 100% correct.
// They are at least the "son" of the interface Command. So the cast type is interface Command.
((Command)it.next()).execute();
}
}
It can be seen that the caller basically only deals with the interface and does not interact with the specific implementation. This also reflects a principle, which is interface-oriented programming. In this way, when adding the fourth specific command in the future, there is no need to modify the code in the caller TestCommand.
After understanding the core principle of the above code, everyone should have their own methods in use, especially in how to separate the caller and specific commands, there are many implementation methods. The above code uses "pass it from List" How to do it. This approach is just for demonstration.
A good reason to use Command mode is also because it can implement Undo functionality, and each specific command can remember the action it just performed and restore it if needed.
Command mode is widely used in interface design. The menu commands in Java Swing use Command mode. Since Java still lacks the performance of interface design, we will not discuss the specific code of interface design. There are many such examples on the Internet.