public class CustomEvent implements java.io.Serializable{An Interface which is a contract between event producers and listeners.
private int eventType = 0;
private String eventDetail = "";
public String getEventDetail () {
return eventDetail;
}
public int getEventType () {
return eventType;
}
public void setEventDetail (String eventDetail) {
this.eventDetail = eventDetail;
}
public void setEventType (int eventType) {
this.eventType = eventType;
}
}
public interface EventListenerIntf {
public void handleEvent (CustomEvent event) throws Exception;
}
An Event Listener
public class EventListener implements EventListenerIntf{A Producer which produces events and have its listener list.
public EventListener () {
EventProducer.addListener (this);
}
private String name = null;
public void handleEvent (CustomEvent event) {
System.out.println ("Event handled by "+ this.name+" : EventType: "+event.getEventType()+" EventDetail :"+event.getEventDetail());
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
}
public class EventProducer {
private static ListlistenerList = new java.util.ArrayList ();
public static synchronized void addListener (EventListener listener) {
listenerList.add (listener);
}
public static synchronized void produceEvents () {
CustomEvent event1 = new CustomEvent ();
event1.setEventType (1);
event1.setEventDetail ("event1 detail");
notifyEventListeners (event1);
CustomEvent event2 = new CustomEvent ();
event2.setEventType (2);
event2.setEventDetail ("event2 detail");
notifyEventListeners (event2);
CustomEvent event3 = new CustomEvent ();
event3.setEventType (3);
event3.setEventDetail ("event3 detail");
notifyEventListeners (event3);
CustomEvent event4 = new CustomEvent ();
event4.setEventType (4);
event4.setEventDetail ("event4 detail");
notifyEventListeners (event4);
}
public static void notifyEventListeners (CustomEvent event) {
for (EventListener listener : listenerList) {
listener.handleEvent (event);
}
}
}
Lets make a simple test these with a simple scenario.
public class Test {
public static void main (String[] args) {
//Listeners getting up !
EventListener amy = new EventListener ();
amy.setName ("Amy");
EventListener april = new EventListener ();
april.setName ("April");
EventListener eli = new EventListener ();
eli.setName ("Eli");
// Producer will create some events
EventProducer.produceEvents();
System.out.println (" good bye");
System.exit (0);
}
}
And out output is :
Event handled by Amy : EventType: 1 EventDetail :event1 detail
Event handled by April : EventType: 1 EventDetail :event1 detail
Event handled by Eli : EventType: 1 EventDetail :event1 detail
Event handled by Amy : EventType: 2 EventDetail :event2 detail
Event handled by April : EventType: 2 EventDetail :event2 detail
Event handled by Eli : EventType: 2 EventDetail :event2 detail
Event handled by Amy : EventType: 3 EventDetail :event3 detail
Event handled by April : EventType: 3 EventDetail :event3 detail
Event handled by Eli : EventType: 3 EventDetail :event3 detail
Event handled by Amy : EventType: 4 EventDetail :event4 detail
Event handled by April : EventType: 4 EventDetail :event4 detail
Event handled by Eli : EventType: 4 EventDetail :event4 detail
good bye
No comments:
Post a Comment