Java commands mode (Command mode)

Source: Internet
Author: User

Command mode is the most let me puzzled a pattern, I read a lot of code, only to feel vaguely grasp its approximate principle, I think understanding design mode is the most important to master the principle of construction, so that their actual programming has a guiding role. The command mode is actually not a very specific, a lot of patterns, it is this flexibility, people are somewhat confuse. command defines a lot of 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 perform some actions.

Encapsulate these commands in a class, and then the user (the caller) then manipulate the class, which is the command pattern, in other words, the user (caller) calls these commands directly, such as opening a document (caller) on a menu, pointing directly to the code that opens the document, using command mode, is to add an intermediary between the two, the direct relationship between the break, while the two are isolated, basically no relationship.

Obviously the advantage of this is that it conforms to the encapsulation characteristics, reduces the coupling degree, and command is the typical pattern that encapsulates the behavior, factory is the pattern that will be created to encapsulate.

From the command mode, I also found that the design pattern a "common problem": it seems like to complicate simple problems, like to add a third party in different classes, of course, this is conducive to the robustness of code maintainability and reusability. How to use the command mode for specific commands pattern code is various, because how to encapsulate commands, different systems, have different practices. The following example is a command encapsulated in a collection list, any object once added to the list, actually loaded into a closed black box, the object's characteristics disappeared, only when taken out, it is possible to blur the resolution.

A typical command pattern requires an interface. There is a unified approach in the interface, which is "encapsulate command/Request as Object":
1  Public Interface Command {2publicabstractvoid  execute (); 3 }   



The specific command/request code is to implement the interface command, there are three specific commands:
1  Public classEngineerImplementsCommand {2      Public voidExecute () {3       //Do Engineer ' s command4 }5 }6 7  Public classProgrammerImplementsCommand {8      Public voidExecute () {9       //Do programmer ' s commandTen } One } A  -  Public classPoliticianImplementsCommand { -     Public voidExecute () { the       //Do politician ' s command - } -}

As a general rule, we can call these three command directly, but using the command mode, we'll wrap them up and throw them into the black box list:
1  Public classproducer{2      Public StaticList producerequests () {3List queue =NewArrayList ();4Queue.add (NewDomesticengineer ());5Queue.add (Newpolitician ());6Queue.add (NewProgrammer ());7       returnqueue;8 }9}

These three commands entered the list, has lost its appearance features, and then removed, may not be able to tell who is engineer who is programmer, see below How to invoke the command mode:
1  Public classTestcommand {2      Public Static voidMain (string[] args) {3List queue =producer.producerequests ();4        for(Iterator it =queue.iterator (); It.hasnext ();)5           //Remove the list of East, other features are not determined, can only guarantee that a feature is 100% correct,6           //They are at least the "son" of the interface command. So the cast type is the interface command7 (Command) It.next ()). Execute ();8 }9}

Thus, the caller basically only deal with the interface, not specific implementation of the interaction, which also embodies a principle, interface programming, so that in the future to add a fourth specific command, you do not have to modify the caller Testcommand in the code.

Understand the core principle of the above code, in use, should each have their own method, especially in how to separate the caller and the specific command, there are many implementations, the above code is the use of "from list over again" approach. This is done only for demonstration purposes.

A good reason to use the command mode is also because it implements the Undo function, and each specific command remembers the action it just performed and recovers as needed.

Command mode is widely used in interface design. Java in the Swing menu commands are used in command mode, because Java in the interface design performance is still missing, so the interface design specific code we do not discuss, there are many such examples on the network.

Java commands mode (Command mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.