Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Programming with Java Lecture 2 The Java Event Model
2
Callbacks and Events Contents Callbacks Events
3
Pauls Pictures Pauls Documents Pauls Sums Pauls Homework Problems Pauls Pictures Pauls Documents Pauls Sums Pauls Homework Pauls ToDo Lists
4
My Documents Reports Papers Presentations Client Window Library File System Calls But I need to tell you something! Slide Shows I’m in charge here, guys!
5
Callbacks Callbacks are used in procedural libraries when they need to handle asynchronous events The alternative is for the client to continuously poll the library for events This is inefficient, especially if a large number of library components are registered with a client This is inefficient, especially if a large number of library components are registered with a client But the use of callback means a client may observe “intermediate” states of a library “Classically” a library’s operations would run to completion before returning control “Classically” a library’s operations would run to completion before returning control
6
Call Sequence ClientaUserLibrary Client installs callback Third party calls library Library invokes callback “I need to tell you something!” “What’s happened?” “He’s changed a name!” Callback queries library “That’s cool” Callback returns Library returns
7
Directory Service public class Directory { public void addEntry(String name, File file) { // pre name != “” and file != null // post File file = map.get(name) } public void removeEntry(String name) { // pre name != “” // post map.get(name) = nil } public void registerNotifier(Notifier n) { // pre n != nil // post n registered, will be called on addEntry and removeEntry }
8
Callbacks and Events Contents Callbacks Events
9
Events An abstraction of Callback that is applicable to “federations” of interacting components The firing of an event is a way of one object telling one or more other recipients that something interesting has happened The sender fires an event The sender fires an event A recipient is called a listener and handles the event A recipient is called a listener and handles the event
10
Java Event Model Event Source Event Listener Register Event Listener Fire Event Event Object Event Object
11
Event Objects Encapsulates information specific to an instance of an event E.g. a “mouse click” event may contain: The position of the mouse pointer The position of the mouse pointer Which mouse button was clicked (and how many times) Which mouse button was clicked (and how many times) The event object is passed as a parameter to the event notification method
12
Event Listeners These are objects that need to be notified when a certain event occurs Event notifications are made through method invocations in the listening object The event object is passed as a parameter The event object is passed as a parameter The event source must know which listener object(s) to call The event source must know which listener object(s) to call This information is contained in an event- listener interface
13
Event Sources Objects that fire events Implements methods that allow listeners to: Register their interest in the events it generates; Register their interest in the events it generates; Unregister their interest in the events it generates. Unregister their interest in the events it generates. Multicast event delivery enables an event to be fired to a number of event-listeners
14
Summary EventObject source getSource() toString() EventListener notification(evt) EventSource addListener() removeListener() fires passed to registers 0..* invokes notifications in0..*
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.