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

1 / 18
About This Presentation
Title:

Java vs 'NET: Event Based Programming

Description:

A Programmer's Introduction to C# by Eric Gunnerson, Apress ... Beta1 implementation seems to support unicast events with properties. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 19
Provided by: donalla
Category:
Tags: net | based | beta1 | event | java | programming

less

Transcript and Presenter's Notes

Title: Java vs 'NET: Event Based Programming


1
Java vs .NET Event Based Programming
  • Monday, 28 May 2001
  • by Donal Lafferty

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
  • Delegation Event Model
  • Used by Java 1.1 and above
  • Four major concepts
  • Event class ? event data
  • Event listener interface ? event callback
  • Event source object ? event publisher
  • Event listener object ? event subscriber
  • Adapter class

Event Source
Event Listener
Event Listener
Event Listener
4
Defining an Event Message
  • Establish what events to listen for
  • Events correspond to an event object class and an
    event listener interface
  • The event object class defines the message
    associated with an event.
  • The event listener interface defines the callback
    associated with each event.
  • Event listener interface can define more than one
    callback, but there is a one to one
    correspondence between callbacks and events.
  • Example event object class
  • Defines the message that will be passed when the
    event occurs. The message is an object.
  • By convention, event object classes are named
    ltxgtEventObject, where ltxgt is the event name.
  • All event object classes have java.util.EventObjec
    t at the root of their inheritance hierarchy.
  • EventObject not interesting other than
    getSource() which returns reference to object
    that generated the event.
  • Ex. public class AlarmEventObject extends
    EventObject
  • public AlarmEventObject(Object source, String
    cause)
  • super(source)
  • alarmCause cause
  • public String getCause() return alarmCause
  • private String alarmCause
  • //class
  • Java programming notes

5
Defining an Event Callback
  • Example event listener interface
  • LevelAlarmListener interface defines the callback
    an event listener object must supply to to
    receive the AlarmEvent
  • By convention, the event listener interface is
    name ltxgtListener, where ltxgt is the event name.
  • All event listener interfaces are derived from
    java.util.EventListener.
  • This is purely a tag interface, and does not
    specify any methods.
  • Ex. public interface LevelAlarmListener
    extends EventListener
  • public announceAlarm(AlarmEventObject event)
  • // interface
  • Java programming notes
  • Anything you derive from is extended.
  • Ex. LevelAlarmListener extends Event
  • If a class implements an interface, the
    interfaces is implemented.
  • Ex. LevelAlarmingServices extends
    AlarmingServices implements LevelAlarmListen
    er, HiLevelAlarmListner
  • NB extends before implements, but the order
    many not be important.

6
Defining an Event Source
  • Event source requirements
  • Support Register, Unregister methods.
  • EventListener objects subscribe to an event using
    a register call, likewise they will unsubscribe
    by unregistrying.
  • By convention registration and unregistration
    methods are named addltxgt and removeltxgt, where ltxgt
    is the listener interface used to define the
    event callback.
  • By taking a listener interface reference as its
    parameter, the register method verifies that
    event subscriber object supports the
    corresponding event listener interface.
  • Track subscribers
  • References to subscriber objects (event listener
    objects) should be stored in a collection object
    such as a vector, however any appropriate
    collection class object can be used.
  • Implement a notification method
  • By convention, takes an event object as a
    parameter.
  • Notification method name can be plain (ex.
    notify), or descriptive (ex. OnMouseDown)
  • Access modifer up to implementers descretion.
  • Traverse subscriber list and send the event
    message message to each subscriber.
  • Different ways to make sure the user does not
    modify the event make it read only, send
    different copies to all subscribers.

7
Defining an Event Source
  • Example event listener interface
  • public class LevelAlarmSource
  • private Vector LevelAlarmListeners new
    Vector()
  • public LevelAlarmEventSource()
  • // Register / Unregister methods
  • public void addLevelAlarmListener(LevelAlarmListe
    ner subscriber)
  • locationListeners.addElement(subscriber)
  • public void removeLevelAlarmListener(LevelAlarmLi
    stner unsubscriber)
  • locationListeners.removeElement(unsubscriber)
  • // notify method
  • protected notify(AlarmEventObject event)
  • // Make copy to subscriber list to prevent
    add/removes
  • Vector recipientList
  • synchronized(this) recipientList (Vector)
    LevelAlarmListeners.clone()
  • for (int i0 ilt recipientList.size() i)

8
Defining an Event Listener
  • Event listener
  • Must implement the interface for the callback of
    the event it wants to subscribe to (an event
    listener can implement multiple event
    interfaces).
  • Must make sure listener object is register with
    event source.
  • Example event listener interface
  • 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 ) // registration
  • myEventSource.notificationTest() // quick test

9
Other Event Concepts
  • Adapter class
  • EventListener may define several callbacks
    logically related to each other.
  • Ex. java.awt.events.KeyListener defines callbacks
    for keyPressed, keyReleased and keyTyped.
  • Adapter class supplies a default implementation
    of several callbacks to facilitate development
    when the user only intends to redefine one
    callback.

10
C Event Model
  • Quick overview
  • C offers a very similar model to the Java
    Delegation Event Model
  • Retains the concept of an event source and an
    event listener/event sink.
  • 1st Class support for events as class members.
  • The reflective API distinguishes events from
    other members of a class such as methods,
    properties and variables.
  • C events are defined in 3 parts
  • Java events required two constructs, the listener
    interface and the event class.
  • Event class
  • Event delegate
  • Event source
  • Need to first understand the concept of a
    delegate.
  • No analogous concept in Java.

11
Delegates
  • Analogous to a C style function pointer
  • Type corresponding to a method signature.
  • The example below defines a delegate-type
    RingAlarm.
  • Ex. delegate void RingAlarm(int alarmValue)
  • Delegate-type variables can be instantiated.
  • Call delegate-type as if it were a class
    constructor.
  • Pass name of fully qualified name of a method
    with the same parameter and return type as the
    delegate.
  • Ex. RingAlarm myAlarmRinger new
    RingAlarm(AlarmListener.AnnounceAlarm)
  • Delegates can be passed as method parameters.
  • Ex. public void TestAlarm(RingAlarm
    myRingerMethod)
  • myRingMethod(12)
  • Supported by C, but not by CLR
  • Delegates mapped to System.Delegate object when
    MIDL is generated.
  • Cant be used to bypass security
  • Security and type checking rules are not fooled
    by delegates. If permission to execute a method
    is not available, using a delegate will not allow
    the method to be called.

12
Delegates cond
  • Multicast delegates
  • If the delegate signature is void, the delegate
    can execute any number of methods, without
    worrying about which value to return to the
    caller.
  • Operators add and remove delegates from a
    delegate
  • Delegates are initially null.
  • adds, while - removes
  • Ex. RingAlarm myAlarmRinger new
    RingAlarm(AlarmListener.AnnounceAlarm)
  • // Add an anonymous delegate to myAlarmRinger
  • myAlarmRinger new(AnotherListener.AnotherAnno
    unceAlarm)
  • // Make a multicast call
  • myAlarmRinger(12)
  • Supported by C, but not by CLR
  • Delegates mapped to System.MulticastDelegate
    object when MIDL is generated.
  • Delegates called in order they were added to the
    multicast delegate.
  • Member delegates
  • Specify access modifier public, protected,
    internal, private
  • Supply delegate type
  • Give the event a name. I.e. an identifier.
  • public class BoringDelegateExample
  • public RingAlarm MyAlarmDelegate

13
Defining an Event Message
  • Event class
  • Identical concept to Java. I.e, defines the
    message (data) that is passed when an event is
    generated.
  • All event classes have System.EventArgs in their
    inheritance hierarchy, which is analogous to
    java.utils.EventObject in Java.
  • Java event constructors always have the event
    source as a constructor parameter used when
    calling the constructor for System.EventArgs can
    be properly called. Instead, C passes this
    information as a separate argument to the event
    callback.
  • Why is the Empty static field of type EventArg?
    Sounds like a circular definition!
  • Naming convention is ltxgtEventArgs, where ltxgt is
    the event name.
  • Java uses ltxgtEventObject, where ltxgt is the event
    name.
  • Example event class
  • public class LevelAlarmEventArgs
    System.EventArgs
  • private string alarmCause
  • public string Cause get() return cause
  • public LevelAlarmEventArgs(string cause)
    alarmCause cause
  • //class

14
Defining an Event Callback
  • Create a delegate to correspond to the event call
    back.
  • Analogous to a Listener interface in Java.
  • For events that do not generate data,
    System.ComponentModel.EventHandler is supplied by
    the SDK.
  • SDK defines other delegates corresponding to many
    events.
  • All event delegates have the same form, which is
    slightly different to the form used in Java
  • Return void.
  • Two parameters, one for the event sender and the
    other for the event message.
  • Convention is to name event delegate
    ltxgtEventHandler, where ltxgt is the event name.
  • Java used ltxgtListener, where ltxgt is the event
    name.
  • Ex. public delegate void LevelAlarmEventHandler(
    object source,
  • LevelAlarmEventArgs e)

15
Defining an Event Source
  • Event source class
  • Events are instance members, with an access
    modifier and a type.
  • Java did not have an explicit event construct, or
    an event keyword.
  • Events are defined when the event keyword is
    applied to the declaration of a multicast
    delegate variable.
  • Delegate is recorded as an event in the class
    structural reflective layer.
  • Ex public event LevelAlarmEventHandler
    OnLevelEvent
  • No registration methods need to be written.
  • Multicast delegate operators and - are used
    instead.
  • Access modifier on event declaration only affects
    access to registration methods.
  • Event keyword makes invokation of event delegate
    to be a private operation.
  • Not required that event delegate be in same class
    that declares the event.
  • But it is handy if a custom event is declared.
  • Example event source
  • public class AlarmSource
  • public AlarmSource()
  • public delegate void LevelAlarmEventHandler(objec
    t source, LevelAlarmEventArgs e)
  • public event LevelAlarmEventHandler
    OnLevelAlarmHandler

16
Defining an Event Source
  • Event source class cond
  • Programming notes
  • Event identifiers in PascalCasing.
  • Event name convention is ltxgtHandler, where ltxgt is
    a descriptive name.
  • null is lower case.
  • Event dispatched by C when event called as if it
    were a method with the associated event object.
    I.e. the event object class specified in the
    event callbacks second argument.
  • Nice convention to name event kick-off methods
    Onltxgt, where ltxgt is the event name.
  • Events supported by C, but not by CLR
  • When MIDL is generated, delegates are made
    privated. and - are mapped to generated
    methods addOn_XXX and removeOn_XXX, where XXX is
    the event identifier.
  • Event callbacks not guaranteed to be delivered in
    a particular order.
  • Gunnersons comments, but details in Essential C
    disagree.
  • If one callback throws an exeception, other
    delegates may not be called.

17
Defining an Event Listener
  • Subscribing to events
  • Listener objects subscribe by registering a
    callback with the event.
  • Create listener class with callback method
  • Ex. public class Detector
  • public Detector ()
  • public void announceLevelAlarm (object source,
    LevelAlarmEventArgs e)
  • System.Console.WriteLine (e.Cause )
  • Create the delegate needed to subscribe to the
    event.
  • Delegates created in a similar fashion to
    objects.
  • Constructor name is same as delegate name
  • Constructor parameter is fully qualified name of
    a static method that has the same parameter
    list and return type as the event delegate
  • Ex. LevelAlarmEventHandler myHandler new
    LevelAlarmEventHandler(Detector.announceAlarm)
  • Register the delegate with the event
  • Registeration done by built in operation
    available for events.
  • Any object with access to the event member can
    register a delegate as an event listener
  • Ex. AlarmSource mySource new AlarmSource()
  • Detector myAlarmDetector new Detector()

18
Events in C
  • Beta2
  • Proper support for delegates as properties will
    allow events to be declared as properties
  • Allows more efficient memory use when an event
    exists, but no subscribers are registered
  • Beta1 implementation seems to support unicast
    events with properties.
Write a Comment
User Comments (0)
About PowerShow.com