A Comparison of Event Models

In the Java 1.0 event model, you use inheritance to customize event handling; essentially, you handle an event in a Component subclass by overriding the action method inherited from the class’s superclass. This is a philosophical break with purely object-oriented design; subclasses should be used to extend (not change) the features derived from a superclass. And, as I discussed above, the action-based event model assumes that all parent containers want to listen to all events fired by all of their child components, which violates the object-oriented concept of encapsulating a function only where it is needed. If nothing else, the Java 1.0 event model is simply inefficient.

Java 1.1 cleanly separates application code, which processes events, from interface code, which generates events. Furthermore, delegation allows programmers to take a structured approach to event handling, an approach that can group related events into hierarchies through the class mechanism. The handling of an event can be targeted to the most appropriate handler, which allows applications to be structured efficiently.

The two event handling models should not be mixed within an application. In general, new programs should conform to the Java 1.1 delegation model, and older code can continue to use the Java 1.0 conventions. The action-based event system will not be removed from Java in the foreseeable future, but the delegation model provides better tools for event encapsulation.

© 1997 by Scott Ladd. All rights reserved.