Java Event Model - Systems and Computer Engineering

Download Report

Transcript Java Event Model - Systems and Computer Engineering

94.204* Object-Oriented Software Development
Part 17
The Java Event Model
•
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
revised March 2002
1
The Java Event Model
• Java 1.1 has introduced a full-blown Java Event Model, which
addresses the same type of needs as Observer/Observable while
solving some of the latter’s limitations
• An event source can now encapsulate all relevant info in an event
object that it sends to its event listeners.
– all listeners implement a common interface defined for the
particular event
– the event source maintains a list of listeners
– when a change occurs, the event source instantiates an event object
and sends it by iterating over the list of listeners and invoking one
of the methods defined in the listener interface
• The Java Event Model is heavily used in GUI design and also in the
JavaBeans component model.
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
2
The Java Event Model
• Let’s rearrange the Prof-Student example so that it
implements the Event Model:
– define a CourseEvent class that extends EventObject
– define a CourseListener interface
– have the Student class implement CourseListener
– have the Prof class (the event source) manage a vector
of courseListeners
• The naming conventions are important!
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
3
The Java Event Model
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
4
Java Event Model: sequence diagrams
1- Subscription
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
5
Java Event Model: sequence diagrams
2- Notification
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
6
The Java Event Model
Implementation:
• take a look at the full signature of the CourseListener
interface methods:
void midtermAnnounced(CourseEvent e);
void midtermPostponed(CourseEvent e);
void assignmentPosted(CourseEvent e);
– they all contain a CourseEvent, which can contain
useful information
– many objects can potentially send a CourseEvent. How
can we determine the source of an event?
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
7
The Java Event Model
• EventObject, the superclass of CourseEvent, provides a
getSource() method.
• The source can be set when instantiating the event, by
passing the event source as an argument:
public void setMidterm(Date date) {
midterm = date;
//the prof creates the event
CourseEvent e = new CourseEvent(this);
//now send it…
…
}
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
8
The Java Event Model
• The event source (the prof) has to manage its list of
listeners (usually a Vector) by providing a
addCourseListener and a removeCourseListener method:
public synchronized void addCourseListener
(CourseListener cl){
courseListeners.addElement(cl);
}
public synchronized void removeCourseListener
(CourseListener cl) {
courseListeners.removeElement(cl);
}
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
9
The Java Event Model
• To send an event, the event source has to iterate through its
list of listeners, and call the appropriate method:
public void setMidterm(Date date) {
midterm = date;
//the prof creates the event and sends it
CourseEvent e = new CourseEvent(this);
Iterator iter=courseListeners.iterator();
while (iter.hasNext()) {
((CourseListener)iter.next()).
midtermAnnounced(e);
}
}
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
10
The Java Event Model
• handling the event:
public void assignmentPosted(CourseEvent e) {
//examine the event source
String name = ((Prof)e.getSource()).getName());
…
// ooh it’s Prof E. !!!
System.out.println("please push it back!");
}
•
CourseEvent could also potentially have other interesting
instance variables (the assignment content, deadline, …)
that could be examined here.
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
11
The Java Event Model
Forces / Consequences:
• all the relevant info is gathered into an event object.
• the listener interface allows us to be more specific about
the particular nature of the event, by providing different
methods for different cases.
• adding a new method to handle a new case means that all
the concrete listeners have to implement one new method.
• the Event source has to manage its list of listeners, which
is a hassle.
Copyright © 2002, Systems and Computer Engineering, Carleton
University. 94.204-17-EventModel.ppt
12