command of design mode (commands)

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 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 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, 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 find the design pattern a "common problem": it seems like to complicate simple problems, Like to add a third party to different classes, of course, this is beneficial to the robustness of the code maintainability and reusability.

How do I use it?
The specific command pattern code is various, because how to encapsulate the commands, different systems, there are different practices. The following example encapsulates a command in a collection list, and when any object is added to the list, it is actually loaded into a closed black box, and the properties of the object disappear. Only when removed, 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":

 Public Interface Command {  publicabstractvoid  execute ();}

The specific command/request code is to implement the interface command, there are three specific commands:

 Public classEngineerImplementsCommand { Public voidExecute () {//Do Engineer ' s command}} Public classProgrammerImplementsCommand { Public voidExecute () {//Do programmer ' s command}} Public classPoliticianImplementsCommand { Public voidExecute () {//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:

 Public class producer{  publicstaticnewnew New    New Programmer  ());      return  queue; }}

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:

 Public class    Testcommand {  publicstaticvoid= producer.producerequests ();          for (Iterator it = queue.iterator (); It.hasnext ();)  // Remove the list in the east, other features are not determined, can only guarantee that a feature is 100% correct,  //  They are at least the "son" of the interface command.  Therefore, the cast type is the interface Command  (command) It.next ()). Execute (); }}

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 just a demonstration.

A good reason to use the command mode is also because it implements the Undo feature. Each specific command remembers the action it just performed and resumes when needed.

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

Reference: Http://www.patterndepot.com/put/8/command.pdf

Transferred from: http://www.jdon.com Banqiao

Command (commands) of design mode (GO)

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.