Linkage between objects - the observer pattern (V)

22.5 Observer pattern with Java event handling

       JDK 1.0 and earlier versions of the event model is based chain of responsibility pattern, but this model is not suitable for complex systems, so the 1.1 JDK versions and later, the event handling model uses the delegate event model based on the observer pattern (DelegationEvent Model, DEM) , that is a Java component event is not triggered by the object that caused the event to be responsible for their own treatment, but delegated to a separate event handler object is responsible .

      In the DEM model, the target role (such as interface components) responsible for issuing event, and observer roles (event handlers) can subscribe to events of interest to its target. When a specific target generates an event, it will notify all subscribers. Posted by an event called the event source (Event Source) , and the subscriber called event listener (Event Listener) , in the process also through the event object (Event Object) to deliver event-related information, in the event listener implementation class implement event handling, so the event listener objects can be called an event handler object. Event source object, the event listener objects (event handling objects) objects and events constitute the Java three elements of event processing model. Event source object acts as observe the target, but the event listener object that acts as an observer. Click the button to the incident as an example, its event process is as follows:

       (1) If the user GUI , click a button that will trigger an event (such as ActionEvent type of action events), the JVM will produce a corresponding ActionEvent type of event object contains information about events and event sources in the event object information, this time the button is the event source object;

       (2) the ActionEvent event object to the event listener object (event processing target), the JDK is provided exclusively for processing ActionEvent interface event of ActionListener , developers required a ActionListener implementation class (e.g. MyActionHandler ), implemented ActionListener interface abstract event handler method declaration actionPerformed () , on the occurrence of an event make the appropriate treatment;

       (3) developers ActionListener interface implementation class (e.g. MyActionHandler ) object is registered to the button, the button class by the addActionListener () to achieve registration method;

       (4) JVM at the time of the triggering event will call button fireXXX () method inside the method call registered to the button event handler object actionPerformed () method to achieve handling of the incident.

       Using a similar approach, we can customize the GUI components, such as the log component comprising two text boxes and two buttons the LoginBean , may be employed FIG 22-6 design shown:

FIG. 22-6 custom login module structure [FIG omitted buttons, text boxes and other interface components]

       FIG. 22-6 related categories as follows:

       (1) LoginEvent event class, which is used to package and information relating to an event, it is not part of the observer pattern, but it can transfer data between the target object and the observer objects, the AWT event model, since all event classes are defined java.util.EventObject subclass.

       (2) LoginEventListener act as abstract viewer, event response method it declares ValidateLogin () , for processing the event, this method is also called event processing method, ValidateLogin () method a LoginEvent type of event object as a parameter, for transmission associated with the event data, the method is implemented in a subclass, the achievement of specific event processing.

       (3) LoginBean acts as a specific target class, where we do not define an abstract class goal, the observer mode for a certain amount of simplification. In LoginBean defined in the abstract observer LoginEventListener types of objects lel objects and events LoginEvent , provides a registration method addLoginEventListener () is used to add an observer, in the Java event handler, a commonly used one to one observer mode, not many observers mode , that is, the target defines only an observer objects, rather than providing an observer to observe a collection of objects. In LoginBean also defines a notification method fireLoginEvent () , the method Java called "firing method" event handling model, in which the interior of an event object instance LoginEvent , the viewer information input by the user to pass an object, and the method call in response to a viewer object ValidateLogin () .

      (4) LoginValidatorA and LoginValidatorB serving as a specific viewer classes that implement the LoginEventListener abstract method declarations in the interface ValidateLogin () , event processing for a particular realization, the method comprising a LoginEvent types of parameters, in LoginValidatorA and LoginValidatorB can classes provide different implementations for the same event.

Published 25 original articles · won praise 1 · views 762

Guess you like

Origin blog.csdn.net/qq_42770949/article/details/104960140