Java and mode Learning Series-Observer Mode

Source: Internet
Author: User

 

I, Observer (observer) mode structure

The observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.

Shows the structure:

II, Application of observation mode in Java

JavaLanguage Support for observer Mode

In the java. util library of the Java language, an observable class and an observer interface are provided to support the observer mode of the Java language. The observer interface defines only one method, namely the update method, which is equivalent to the abstract observer role. The observable class is equivalent to an abstract topic role.

AWTDEM inMechanism

In AWT 1.1 and later versions, the event processing model is a delegate event model (delegation event model or Dem) based on the observer mode ). In the DEM model, the subject role is responsible for releasing events, and the observer role subscribes to events of interest to a specific topic. When an event is generated for a specific topic, it notifies all interested subscribers. In dem, publishers are called event sources, while subscribers are called event listeners.

III, Advantages and disadvantages of observer Mode

Advantages: (1) The observer mode establishes an abstract coupling between the subject and the observer, achieving loose coupling. (2) The observer mode supports broadcast communication. The topic role sends a notification to all registered observers.

Disadvantages: (1) If a topic has many observers, it takes a lot of time to notify all observers. (2) If the notification to the observer is asynchronously delivered through another thread, it must be ensured that the delivery is carried out in the correct way.

IV, Observation mode topics

1, DemStructure

In common event processing models, objects are generally divided into three types: Event objects, event producer objects (event source objects), and event receiver objects. Generally, the event object encapsulates information about the event. When the internal state of the event maker changes, an event object that represents the state change will be created as needed, and send it to all the registered event recipient objects.

Event source object: To become an event source, a class does not need to implement any interface or inherit any class, but an event source must maintain a list of event listeners. Call addxxxlistener () to add a listener and call removexxxlistener () to delete a listener. All AWT components are subclasses of Java. AWT. component. They all inherit the addxxxlistener () methods from the component class.Event object:In dem, each event corresponds to an event object, and all the event objects in AWT are from Java. util. the object inherits, and each specific event object has some additional functions. The event object encapsulates the event information required by the event source object and the event listener object. Some event classes, such as paintevent, will not be passed to the listener, so it is of no use to Java programmers. Java Application designers will be exposed to the event classes that will be passed to the listener. Such as actionevent, adjustmentevent, componentevent, containerevent, focusevent, itemevent, keyevent, mouseevent, textevent, and windowevent.

Event listener object

The event listener object is called when an event occurs. To make an object an event listener object, you must implement the event listener interface. All the event listener interfaces in the AWT library are subinterfaces of the Java. util. eventlistener interface. In AWT, some event listener interfaces declare more than one method. AWT provides these interfaces with the corresponding adapter class called the event listener adapter class. The adapter class provides empty implementations for these interfaces. Therefore, an application class that needs to process an event only needs to inherit the corresponding event adapter class and replace the event processing method of interest. These event listening adapters are the applications in the default adapter mode.

Semantic events and underlying events

AWT distinguishes between underlying events and semantic events. A semantic event represents a user's event. For example, if you click a button with the mouse, it is a socialist event, and an underlying event is a simple event that constitutes a variety of semantic events. For example, a mouse click event is composed of a mouse key press event and a mouse key release event. The four most important semantic event classes in AWT: actionevent (click a button, select a menu item, select a list item, and enter text in the text box) adjustmentevent (adjust the scroll bar) itemevent (you can select one from a set of selection boxes or select one from the list) textevent (text box content changes) has six underlying event classes: componentevent, keyevent, mouseevent, focusevent, windowevent, and containerevent.

2, SwingTimer

Java provides the java. util. Timer class and javax. Swing. Timer class.

Util. TimerTimer and swing. TimerTimer difference

Util timer is a timer designed for general purposes, while swing timer is designed for Swing library packages. A swing timer is scheduled for any number of operations in a thread. It is often used on the user interface, such as changing the cursor in the text box, regularly displaying and hiding tooltips.

(1) thread security

Like AWT, swing is NOT thread-safe. They use a single thread to process all events from the operating system.

(2) Performance

The swing Timer class uses a thread to process all timer objects, so it is not suitable to process a large number of timer objects at the same time; while the util timer is not, it provides an independent thread for each timer object, therefore, if the designed system requires a large number of timer objects, util timer should be used. At the same time, since all the timer objects of the swing Timer class use the same thread as the event processor, it is suitable for use in swing components.

(3) differences in use

When using the swing Timer class, you need to create an action object to encapsulate scheduled behaviors, while the util timer needs to create a timertask object for each scheduled task.

 

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.