< Day Day Up > |
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 > |