
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
426
|
Chapter 19: The Delegation Event Model
public function onChatMessage (e:ChatEvent):Void;
public function onUserNameChanged (e:ChatEvent):Void;
}
Notice that the event object passed to each event method is a ChatEvent instance.
One of the event listener classes in our Chat example might be ChatGUI, a class
responsible for displaying the chat interface on screen. The ChatGUI class would
implement both onChatMessage( ) and onUserNameChanged( ). Here’s a skeletal
example of what the ChatGUI class might look like:
class ChatGUI implements ChatListener {
// Properties and methods used to render the interface not shown.
// Constructor also not shown.
public function onChatMessage (e:ChatEvent):Void {
displayMessage(e.messageSender, e.messageText);
}
public function onUserNameChanged (e:ChatEvent):Void {
updateUserList(e.oldUserName, e.newUserName);
}
}
Are Observer and Delegation Event Model Equivalent?
Structurally, the delegation event model has much in common with the Observer
pattern. The Observer pattern’s two basic participants—the subject class and the
observer classes—correspond to the delegation event model’s event source class,
which broadcasts events, and event listener classes, which receive event notifica-
tions. But are the two designs the same?
Not exactly. The delegation ...