698: Sidetrip
Programming languages that are not object oriented, such as C, support
the callback style of user-interface programming. When using the
callback style, you connect specific procedures to hooks provided for the
particular events generated by specific components.
Java offers the much more powerful delegation style of user-interface
programming. Each component delegates to connected listeners responsibility
for responding to events. Use of listeners confers several virtues:
- Listener instances belong to classes that populate a class hierarchy.
Thus, the definition of a listener class can refine a portion of the
behavior defined in that class's superclasses, while simply reusing
other behavior. The example in Segment 697 illustrates.
- Listener instances can be connected and disconnected from components
dynamically, at run time.
- Listener instances can maintain state. For example, a listener instance
can keep track of the number of times that it responds to an event.