Viewing design Patterns from Java class libraries (2)

Source: Internet
Author: User
Tags event listener

In the previous section, we talked about what patterns are, what design patterns are, and how to observer a design pattern. We believe that the concept of the model should be a comparative understanding. This part and later content, will step into the topic, from the Java Class Library analysis to start to describe how the design pattern is applied to a perfect design. In fact, the Java class Library is so complicated that it's not possible to enumerate all the examples of design patterns that can be found, but to find examples that are easy to find. In fact, there is no need, because as long as there is enough understanding of a design pattern, for its specific application, it is not very difficult things.

Command mode

When designing general purpose software, a lot of the tricks in C or C + + are a callback function (Callback), called a callback function, that registers a function somewhere in the system, letting the system know that the function exists, and then, when an event occurs, This function is called again to respond to the event. In C or C + +, the implementation of the callback function method is to use the function pointer. In Java, however, pointers are not supported, so there is a command pattern, an object-oriented version of this callback mechanism.

Command mode is used to encapsulate an order/request, and, simply put, a command object contains a sequence of actions (statements) to be executed to perform a specific task. Of course, not a random statement sequence can constitute a command object, according to the command pattern design, the command object and its caller incvoker should have interface conventions. That is, Invoker gets a reference to the Command object and invokes the method defined therein, and when the Command object changes (or if the object itself changes, or simply completely another command object), the code in the invoker can be changed. In this way, the implementation of the task and the task can be separated by encapsulating the request.

Figure II: Class diagram for Command mode

There are two different ways of handling requests, one being that the command acts only as an agent, forwards the request to one of the recipient objects, and the command object handles all of the request operations on its own. Of course, this is only two extremes, and more often the command completes a portion of the work, while the other part is given to the recipient object to handle.

In the new JDK's proxy event model, it can be seen as such a command pattern. In that model, an event listener class EventListener listens to an event and implements a specific action based on the interface definition. For example, when a documentlistener is registered with the Adddocumentlistener (Documentlistener Listener) method of the Document object, The Insertupdate (Documentevent e) method implemented in Documentlistener is invoked later if a text insertion event occurs in the Document object, and if a text deletion event occurs, removeupdate ( Documentevent e) method is invoked. How about, think about, this is not a command mode of application?

However, the most classic command mode is the application of the action interface in swing. The action actually inherits the ActionListener, which means it is also an event listener (EventListener). But the action, as a actionlistener extension mechanism, provides more functionality. It can contain a description of one or more words or icons for this action action, which provides a enable/disable sign of functionality. Also, an action object can be shared by multiple invoker, such as buttons that implement the same function, menus, and shortcuts. These invoker know how to add an action and take full advantage of the extension mechanism it provides. It can be said that the action here is more like an object, because it not only provides the implementation of the method, but also provides the description and control of the method. It is convenient to describe any transaction, which is the power of object-oriented methods.

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.