Java vs 'NET: Event Based Programming - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Java vs 'NET: Event Based Programming

Description:

A Programmer's Introduction to C# by Eric Gunnerson, Apress. Java in a Nutshell by By David Flanagan, O'Reilly. Reference Manuals .NET documentation ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 20
Provided by: donalla
Category:

less

Transcript and Presenter's Notes

Title: Java vs 'NET: Event Based Programming


1
Java vs .NETEvent Based Programming
  • by Donal Lafferty, DSG TCD.
  • Donal.Lafferty_at_cs.tcd.ie
  • Wednesday, 13 June 2001

2
References
  • Web examples
  • The Java 1.1 Event Model by Gopalan Suresh Raj
  • http//www.execpc.com/gopalan/java/dragger.html
  • Books
  • A Programmers Introduction to C by Eric
    Gunnerson, Apress
  • Java in a Nutshell by By David Flanagan, OReilly
  • Reference Manuals
  • .NET documentation
  • http//msdn.microsoft.com/library/default.asp?URL
    /library/dotnet/cpref/cpref_start.htm
  • Examples
  • tcdIO source code
  • \\malinhead\projects\TORJC\

3
Java Event Model
Event Source Object
  • Delegation Event Model
  • Event message class
  • Event listener interface
  • Event source object
  • Event listener object
  • Adapter class

Message
Message
Message
Listener Object
Listener Interface
Listener Object
Listener Interface
Listener Object
Listener Interface
4
Defining an Event Message
  • Example event object class
  • public class AlarmEventObject extends EventObject
  • public AlarmEventObject(Object source, String
    cause)
  • super(source)
  • alarmCause cause
  • public String getCause() return alarmCause
  • private String alarmCause
  • //class

Message
5
Defining an Event Callback
  • Example event listener
  • interface
  • public interface LevelAlarmListener extends
    EventListener
  • public void announceAlarm(AlarmEventObject
    event)
  • // interface

Listener Interface
6
Defining an Event Source
  • Track subscribers
  • public class LevelAlarmSource
  • public LevelAlarmEventSource()
  • private Vector LevelAlarmListeners new
    Vector()
  • Support Register, Unregister methods.
  • // Register / Unregister methods
  • public void addLevelAlarmListener(LevelAlarmListe
    ner subscriber)
  • locationListeners.addElement(subscriber)
  • public void removeLevelAlarmListener(LevelAlarmLi
    stner unsubscriber)
  • locationListeners.removeElement(unsubscriber)

Event Source Object
7
Defining an Event Source
  • Implement a notification method
  • By convention, takes an event object as a
    parameter.
  • Traverse subscriber list, send event message
  • to each subscriber.
  • // notify method
  • protected notify(AlarmEventObject event)
  • // Make copy to subscriber list to prevent
    add/removes
  • Vector recipientList
  • synchronized(this)
  • recipientList (Vector) LevelAlarmListeners.clo
    ne()
  • for (int i0 ilt recipientList.size() i)
  • LevelAlarmListener listener
  • (LocationListener) recipientList.elementAt(i)
  • listener.announceAlarm(event)

Event Source Object
8
Defining an Event Listener
  • Event listener
  • Class definition
  • public class AlarmListener implements
    LevelAlarmListener
  • public AlarmListener()
  • public void announceAlarm(AlarmEventObject
    event)
  • System.out.println(event.getCause() )
  • Usage
  • public class MyProg
  • public static void main(String args)
  • LevelAlarmSource myEventSource
    LevelAlarmSource() // publisher
  • AlarmListener myEventListener
    AlarmListener() // subscriber
  • myEventSource.addLevelAlarmListener(
    myEventListener )// reg. myEventSource.notificati
    onTest() // quick test
  • // main
  • // class

Listener Object
Listener Interface
9
Other Event Concepts
  • Adapter class
  • Listener interface defines several callbacks
    logically related.
  • Adapter class supplies a default implementation
    of several callbacks.

10
C Event Model
Event Source Object
  • Similar to Java
  • 1st Class support for
  • events as class members
  • 3 parts
  • Event class
  • Event delegate
  • Event source

Message
Message
Message
Listener Object
Delegate
Listener Object
Delegate
Listener Object
Delegate
11
Delegates
  • Analogous to a C style function pointer
  • Type corresponding to a method signature
  • delegate void RingAlarm(int alarmValue)
  • Delegate-type variables can be instantiated.
  • RingAlarm myAlarmRinger
  • new RingAlarm(AlarmListener.AnnounceAlarm)
  • Delegates can be passed as method parameters
  • public void TestAlarm(RingAlarm myRingerMethod)
  • myRingMethod(12)
  • //2

12
Delegates cond
  • Multicast delegates
  • RingAlarm myAlarmRinger new RingAlarm(AlarmListe
    ner.AnnounceAlarm)
  • Operators to add and remove methods from a
    delegate
  • // Add an anonymous delegate to myAlarmRinger
  • myAlarmRinger new(AnotherListener.AnotherAnnoun
    ceAlarm)
  • // Make a multicast call
  • myAlarmRinger(12)
  • // Remove a delegate
  • myAlarmRinger - new(AnotherListener.AnotherAnnoun
    ceAlarm)
  • //2

13
Delegates cond
  • Member delegates
  • Specify access modifier public, protected,
    internal, private
  • Supply delegate type
  • Give the delegate an identifier.
  • public class BoringDelegateExample
  • public RingAlarm MyAlarmDelegate

14
Defining an Event Message
  • Example event object class
  • public class LevelAlarmEventArgs
    System.EventArgs
  • private string alarmCause
  • public string Cause get() return cause
  • public LevelAlarmEventArgs(string cause)
  • alarmCause cause
  • //class3

Message
15
Defining an Event Callback
  • Create a delegate
  • public delegate void LevelAlarmEventHandler(object
    source,
  • LevelAlarmEventArgs
    e) //4

Delegate
16
Defining an Event Source
  • Event source class
  • public class AlarmSource
  • public AlarmSource()
  • public delegate void LevelAlarmEventHandler(
  • object source,
    LevelAlarmEventArgs e)
  • public event LevelAlarmEventHandler
    OnLevelAlarmHandler
  • protected void OnLevelAlarm(LevelAlarmEventArgs
    e)
  • if (OnLevelAlarmHandler ! null) // I.e. at
    least one subscriber
  • OnLevelAlarmHandler(e) // Dispatch handled
    by C
  • public void TestEvent(LevelAlarmEventArgs e)
    OnLevelAlarm(e)
  • //class

Event Source Object
17
Defining Event Source cond
  • Caveats
  • Access modifier on event definition
  • Events native to C, not CLR (as per delgates)
  • If one callback throws an exeception, other
    delegates may not be called.
  • Event callbacks not guaranteed to be delivered in
    a particular order.

18
Defining an Event Listener
  • Create listener class that implements the
    callback method
  • Delegate
  • public delegate void LevelAlarmEventHandler(object
    source,

  • LevelAlarmEventArgs e)
  • Event listener (sink)
  • public class Detector
  • public Detector ()
  • public void announceLevelAlarm (object source,
    LevelAlarmEventArgs e)
  • System.Console.WriteLine (e.Cause )

19
Registering an Event Listener
  • Create delegate needed to subscribe to event.
  • ...
  • Detector myDetector new Detector()
  • LevelAlarmEventHandler myHandler
  • new LevelAlarmEventHandler(myDetector.announceAla
    rm)
  • ...
  • Register the delegate with the event
  • ...
  • AlarmSource mySource new AlarmSource()
  • mySource.OnLevelAlarmHandler myHandler
  • ...
Write a Comment
User Comments (0)
About PowerShow.com