140 likes | 150 Views
Component-Based Software Engineering. The Java Event Model Paul Krause. Problems. Pauls Pictures Pauls Documents Pauls Sums Pauls Homework. Pauls Pictures Pauls Documents Pauls Sums Pauls Homework. Pauls ToDo Lists. Lecture 4 - Interfaces and Events. Contents
E N D
Component-Based Software Engineering The Java Event Model Paul Krause
Problems • Pauls Pictures • Pauls Documents • Pauls Sums • Pauls Homework • Pauls Pictures • Pauls Documents • Pauls Sums • Pauls Homework • Pauls ToDo Lists
Lecture 4 - Interfaces and Events Contents • Interfaces and Callbacks • Events
Client • My Documents • Reports • Papers • Presentations Window Library But I need to tell you something! I’m in charge here, guys! File System Slide Shows Calls
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 • 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
Client aUser Library Client installs callback Third party calls library “I need to tell you something!” “What’s happened?” “He’s changed a name!” “That’s cool” Callback returns Library returns Call Sequence Library invokes callback Callback queries library
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 } }
Lecture 7 - Interfaces and Events Contents • Interfaces and Callbacks • Events
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 • A recipient is called a listener and handles the event
Register Event Listener Fire Event Java Event Model Event Source Event Object Event Listener
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 • Which mouse button was clicked (and how many times) • The event object is passed as a parameter to the event notification method
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 source must know which listener object(s) to call • This information is contained in an event-listener interface
Event Sources • Objects that fire events • Implements methods that allow listeners to: • Register 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
EventSource EventListener EventObject source passed to addListener() removeListener() fires getSource() toString() notification(evt) registers 0..* invokes notifications in 0..* Summary