
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
423
Chapter 19
CHAPTER 19
The Delegation Event Model
[In the delegation event model,] an event is propagated from a “Source” object to a
“Listener” object by invoking a method on the listener and passing in the instance of
the event subclass which defines the event type generated.
——from Sun’s Java AWT: Delegation Event Model (http://
java.sun.com/j2se/1.4.1/docs/guide/awt/1.3/designspec/
events.html)
In Chapter 16, we used the Observer pattern to structure a group of objects so that
when one changes state, the others are automatically notified. As we learned in
Chapter 16, the Observer pattern is intentionally generic, emphasizing loose cou-
pling between the object that changes (the subject) and the objects being notified of
the change (the observers). In this chapter, we’ll apply the concepts of the Observer
pattern to a more specific situation: implementing events for a class. Our event
implementation will follow Java’s delegation event model, a general design for event
broadcasting.
The delegation event model framework we’ll develop in this chapter can be used to
implement event broadcasting for any class. For example, a Chat class might use it to
implement onChatMessage( ) or onUserNameChanged( ) events. An interactive
WorldMap class might likewise implement onCountryClick( ), onCountryRollover( ...