Chapter 19. The Delegation Event Model

 <  Day Day Up  >  

[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 coupling 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( ), and onCountryRollout( ) events. Or a ChessGame class might implement onCheckMate( ) , onPieceTaken( ) , and onMove( ) events. In each case, the delegation event model provides a basic structure for the event source ( Chat , WorldMap , and ChessGame ) and the event listeners (objects that register to receive events from an event source).

As usual, the code discussed in this chapter is available at http:// moock .org/eas2/examples.

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net