Confused about custom events

Discussion in 'Java' started by -, Jul 20, 2005.

  1. -

    - Guest

    I have a ConnnectionListener that has connectionOpened and
    connectionClosed methods.

    I have a ClientConnectionListener that extends ConnectionListener and
    has several other specific methods.

    I have a Connection class that has
    addConnectionListener(ConnectionListener cl) {
    ...add(ConnectionListener.class, cl);
    }

    It seems that adding it requires "ConnectionListener.class".
    Does this mean that my ClientConnection has to :

    1) Override the addConnectionListener and change it to
    "ClientConnectionListener.class" or

    2) Create a method
    addClientConnectionListener(ClientConnectionListener...) ?
     
    -, Jul 20, 2005
    #1
    1. Advertising

  2. On Wed, 20 Jul 2005 13:16:09 +0800, - wrote:

    > I have a ConnnectionListener that has connectionOpened and
    > connectionClosed methods.
    >
    > I have a ClientConnectionListener that extends ConnectionListener and
    > has several other specific methods.
    >
    > I have a Connection class that has
    > addConnectionListener(ConnectionListener cl) {
    > ...add(ConnectionListener.class, cl);
    > }
    >
    > It seems that adding it requires "ConnectionListener.class".
    > Does this mean that my ClientConnection has to :
    >
    > 1) Override the addConnectionListener and change it to
    > "ClientConnectionListener.class" or
    >
    > 2) Create a method
    > addClientConnectionListener(ClientConnectionListener...) ?


    If you want to fire the events only ClientConnectionListener has, yes. If
    you, however, want to use a ClientConnectionListener just as if where a
    normal ConnectionListener, then there is no problem at all using the
    simple menthod.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jul 20, 2005
    #2
    1. Advertising

  3. -

    - Guest

    Stefan Schulz wrote:
    > If you want to fire the events only ClientConnectionListener has, yes. If
    > you, however, want to use a ClientConnectionListener just as if where a
    > normal ConnectionListener, then there is no problem at all using the
    > simple menthod.


    As mentioned in the EventListenerList class:

    Quote:
    EventListenerList listenerList = new EventListenerList();
    FooEvent fooEvent = null;

    public void addFooListener(FooListener l) {
    listenerList.add(FooListener.class, l);
    }

    public void removeFooListener(FooListener l) {
    listenerList.remove(FooListener.class, l);
    }


    // Notify all listeners that have registered interest for
    // notification on this event type. The event instance
    // is lazily created using the parameters passed into
    // the fire method.

    protected void fireFooXXX() {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
    if (listeners==FooListener.class) {
    // Lazily create the event:
    if (fooEvent == null)
    fooEvent = new FooEvent(this);
    ((FooListener)listeners[i+1]).fooXXX(fooEvent);
    }
    }
    }
    Unquote


    Notice that FooListener.class is being used both for the add/remove and
    fireFooXX methods.

    If I have SubclassFooListener and add to addFooListener(FooListener l) ,
    calling fireFooXXX is of no use since it only checks for
    FooListener.class and not SubclassFooListener.class.

    I couldn't find any existing listeners in the java api that actually
    subclasses another listener... Is it because it's not supposed to?
     
    -, Jul 20, 2005
    #3
  4. On Wed, 20 Jul 2005 14:19:55 +0800, - wrote:

    > public void addFooListener(FooListener l) {
    > listenerList.add(FooListener.class, l);
    > }


    Explicitly uses FooListener, not l.getClass();

    > public void removeFooListener(FooListener l) {
    > listenerList.remove(FooListener.class, l);
    > }


    Explicitly uses FooListener, not l.getClass();

    > If I have SubclassFooListener and add to addFooListener(FooListener l) ,
    > calling fireFooXXX is of no use since it only checks for
    > FooListener.class and not SubclassFooListener.class.


    That is right. Should it? After all, by calling addFooListener, for the
    purposes of this one class, all subclasses of FooListener collapse to
    FooListeners.

    > I couldn't find any existing listeners in the java api that actually
    > subclasses another listener... Is it because it's not supposed to?


    It is usually not really necessary, since most events do not really lend
    themselves to extension, but i don't see why it should not be possible.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jul 20, 2005
    #4
  5. -

    - Guest

    Stefan Schulz wrote:
    >>If I have SubclassFooListener and add to addFooListener(FooListener l) ,
    >>calling fireFooXXX is of no use since it only checks for
    >>FooListener.class and not SubclassFooListener.class.

    >
    >
    > That is right. Should it? After all, by calling addFooListener, for the
    > purposes of this one class, all subclasses of FooListener collapse to
    > FooListeners.


    It's not the case.


    public class Foo {

    void addFooListener(FooListener fl) {
    list.add(FooListener.class, fl);
    }

    ...
    }

    public interface FooListener {

    }

    public interface SubFooListener extends FooListener {

    }

    For the following:

    1. Foo.addFooListener(new FooListener());
    2. Foo.addFooListener(new SubFooListener()));

    when fireFoo is called, only '1' succeeds as

    the statement inside fireFoo: listener == FooListener.class
    only checks for FooListener class.

    It's not identical to 'instanceof' where one would expect SubFooListener
    to be detected as FooListener.
     
    -, Jul 20, 2005
    #5
  6. -

    Roedy Green Guest

    On Wed, 20 Jul 2005 13:16:09 +0800, - <> wrote or
    quoted :

    >It seems that adding it requires "ConnectionListener.class".
    >Does this mean that my ClientConnection has to :
    >
    >1) Override the addConnectionListener and change it to
    >"ClientConnectionListener.class" or
    >
    >2) Create a method
    >addClientConnectionListener(ClientConnectionListener...) ?


    no. This is because every ClientConnectionListener is also a
    ConnectionLister. ClientConnectionListener meets the criteria for
    addConnectionListener().

    This wonderful feature is much of what drove us to OO in the first
    place.


    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 23, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    643
    Bruno Alexandre
    May 24, 2006
  2. mosscliffe
    Replies:
    0
    Views:
    353
    mosscliffe
    Jun 20, 2006
  3. Replies:
    2
    Views:
    338
  4. kellygreer1
    Replies:
    5
    Views:
    43,694
    Phil H
    Feb 6, 2008
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    420
    Chris
    Aug 30, 2005
Loading...

Share This Page