Transcript Lecture 5

Internet Software
Development
The Java Event Model
Lecture 5
The Java Event Model
Contents
 Introduction to Events
 The Java Event model
 Using this model to notify Customers that
Pizzas are ready from a Bakery
 This example is taken from JavaBeans by
Example, by Henri Jubin, Prentice Hall
Problems
 Pauls Pictures
 Pauls Documents
 Pauls Pictures
 Pauls Sums
 Pauls Documents
 Pauls Homework
 Pauls Sums
 Pauls Homework
 Pauls ToDo Lists
Window Library
Client
I’m in charge
here, guys!
But I need to
tell you
something!
My Documents
Reports
Papers
Presentations
Slide Shows
Calls
File System
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
The Java Event Model
Contents
 Introduction to Events
 The Java Event model
 Using this model to notify Customers that
Pizzas are ready from a Bakery
 This example is taken from JavaBeans by
Example, by Henri Jubin, Prentice Hall
Java Event Model
Event Source
Register Event Listener
Event
Object
Fire Event
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 eventlistener interface
Event Sources
 Objects
that fire events
 Implement methods that allow listeners to:


Register their interest in the events they
generate;
Unregister their interest in the events they
generate.
 Multicast
event delivery enables an event
to be fired to a number of event-listeners
Summary
EventObject
source
EventSource
fires
getSource()
toString()
registers
addListener()
removeListener()
invokes notifications in
passed to
0..* EventListener
0..*
notification(evt)
The Java Event Model
Contents
 Introduction to Events
 The Java Event model
 Using this model to notify Customers that
Pizzas are ready from a Bakery
 This example is taken from JavaBeans by
Example, by Henri Jubin, Prentice Hall
Java Event Model
Event Source
Register Event Listener
Event
Object
Fire Event
Event Listener
Chili PizzaExpress
EventObject
«interface»
OrderListener
source
getSource()
toString()
Bakery
fires
PizzaEvent
registers with
addOrderListener()
removeOrderListener()
sendMessage(PizzaEvent)
invokes notifications in
pizzaStatus(evt)
passed to
0..*
0..*
Customer
iNumber
iSliceNumber
run( )
PizzaEvent.java
import java.util.EventObject;
EventObject
source
getSource()
toString()
PizzaEvent
public class PizzaEvent extends EventObject
{
public PizzaEvent(Object aSource) {
super(aSource);
}
}
OrderListener.java
import java.util.EventListener;
EventListener
OrderListener
pizzaStatus
public interface OrderListener extends
EventListener {
public void pizzaStatus(PizzaEvent anEvent);
}
Bakery.java
Bakery
addOrderListener
removeOrderListener
sendMessage(PizzaEvent)
public class Bakery
public Bakery( ) {
// constructor of a Bakery instance
}
public addOrderListener( eL ) {
// inserts OrderListeners in some
// structure
}
public removeOrderListener( eL ) {
// deletes OrderListeners from that
// structure
}
private void sendMessage( evt ) {
// broadcast evt somehow
}
Properties of Bakery.java
import java.lang.Thread;
import java.util.*;
public class Bakery implements Runnable {
private Vector iCustomers = new Vector( );
private Thread iThread;
// methods go here…
}
Constructor for Bakery
public Bakery ( ) {
iThread = new Thread(this);
iThread.start( );
}
// When a new instance of Bakery is created,
// a flow of control owned by it is started.
The main flow of control
public void run( ) {
while(true) {
iThread.sleep(4000);
PizzaEvent event = new PizzaEvent(this);
sendMessage(event);
}
}
// a Bakery broadcasts a message that Pizza is ready
// every 4 seconds
Adding and removing
Listeners
public void addOrderListener(OrderListener aListener) {
iCustomers.addElement(aListener);
}
// Remember iCustomers is a Vector field in Bakery
public void removeOrderListener(OrderListener aListener) {
iCustomers.removeElement(aListener);
}
Broadcasting the message
private void sendMessage(PizzaEvent anEvent) {
Vector v;
v = iCustomers.clone( );
for (int i = 0; i<v.size( ); i++) {
OrderListener ol = v.elementAt(i);
ol.pizzaStatus(anEvent); // implement in Customer
}
System.out.println(“Pizza ready …”);
}
Summary for Sources
 Record
all the references to Listener
Objects in a “Vector”
 Register Listeners by adding their name to
the Vector
 Unregister Listeners by removing their
name from the Vector
 Step through the elements of the Vector to
notify all the Listeners
The Story so Far
EventObject
«interface»
OrderListener
source
getSource()
toString()
Bakery
fires
PizzaEvent
registers with
addOrderListener()
removeOrderListener()
sendMessage(PizzaEvent)
invokes notifications in
pizzaStatus(evt)
passed to
0..*
0..*
Customer
iNumber
iSliceNumber
run( )
The Customer Class
 A Customer
also has its own flow of
control
public class Customer implements OrderListener,
Runnable {
private int iNumber;
// identify customer
private boolean iHaveSlice;
// something to eat?
private Thread iThread;
// identifiy flow of control
private Randon iRandom;
// gaps between bites
private int iSliceNumber;
// Slices eaten
…}
Construct a Customer
public Customer(int aNumber) {
iNumber = aNumber;
iRandom = new Random(aNumber);
iThread = new Thread(this);
iThread.start( );
}
// Construct a Customer with a specified identifier, and
// start its own flow of control
Making your Customer Run
public void run( ) {
while(true) {
if (iHaveSlice) {
for (int bites=0; bites<4; bites++) {
System.out.println(“customer: “ + iNumber +
bites + “ slice:” + iSliceNumber);
iThread.sleep(iRandom.nextFloat( ) * 3000);
}
iHaveSlice = false;
iThread.suspend( );
}
}
// Takes 4 bites, with a rest between each, then
}
// waits for some more Pizza.
Response to PizzaEvents
 Remember,
we invoked a method called
“pizzaStatus” when we broadcast
messages from the Bakery. Customer
must implement this:
public void pizzaStatus(PizzaEvent anEvent) {
if ( ! iHaveSlice) {
iHaveSlice = true;
iSliceNumber++;
iThread.resume( );
}
Warning


These slides have simplified the implementation a little
bit
We have missed out:




Explicit type conversions;
“Synchronisation” of critical sections in threads
The full implementation can be found on the CS288 Web
site
This is taken from JavaBeans by Examples, Henri Jubin,
Prentice Hall
Running the Bakery
public class TestApp {
public static void main(String args[ ]) {
TestApp t = new TestApp( );
}
public TestApp( ) {
Bakery b = new Bakery( );
Customer c1 = new Customer( 1 );
Customer c2 = new Customer( 2 );
b.addOrderListener( c1 );
b.addOrderListener( c2 );
}
}
Summary
 We
have explored a simple example of a
general Notifier-Observer design pattern
 Everything in this example is available in
the Java 2 Software Development Kit
 The trick has been to use a design pattern
that allows as many Observers
(Customers, in our case) to be added as
required