Listeners and Events

Discussion in 'Java' started by SMMT, Nov 9, 2004.

  1. SMMT

    SMMT Guest

    Hi,

    I'm building and application and I'm trying to build it upon MVC concepts. For this I use quite a
    lot listeners and events. I implement the events as classes, the listenrs as interfaces , but the
    trigger process is worring me.

    I do a code like this

    public class EventCreator {

    private Collection listeners = new HashSet();
    public void addEventListener(EventListener l){
    listeners.add(l);
    }
    private void fireEvent(Event e){
    for (Iterator it = listeners.iterator();it.hasNext();){
    ((EventListener)it.next()).startEvent(e);
    }
    }
    }

    I iterate the collection of listeners and call the listenre method for the particular event.
    I'm gessing this is not the best way to implement this, so I'm asking for opinions on this.
    I wonder if it is a good practice to create a separated thread so I can move to the next listener
    without having to wait for the fist to resolve its event handling code. And if so if there are any
    API to facilitate the work. Also if storing the listeners in a collection is a good ideia (I see no
    other) and if so, witch implementation is the fastest for iterating porposes...

    Thank you for your time.
     
    SMMT, Nov 9, 2004
    #1
    1. Advertising

  2. SMMT wrote:
    > I iterate the collection of listeners and call the listenre method for
    > the particular event.
    > I'm gessing this is not the best way to implement this, so I'm asking
    > for opinions on this.


    This is how it's generally done, so it can't be that bad.


    > I wonder if it is a good practice to create a separated thread so I can
    > move to the next listener without having to wait for the fist to resolve
    > its event handling code.


    Usually, you put the burden on the listener. It's a well-known convention
    that listener code should not do anything time-consuming - OR spawn a thread
    if it needs to. After all, most listeners probably do very little work in the
    listener method, and spawning a thread represents a considerable overhead in
    its own right:

    > And if so if there are any API to facilitate
    > the work. Also if storing the listeners in a collection is a good ideia
    > (I see no other) and if so, witch implementation is the fastest for
    > iterating porposes...


    This is almost certainly not performance relevant, but I'm pretty sure
    ArrayList has the fastest iteration (of course it's bad if you have
    a lot of listeners and want to remove one).
     
    Michael Borgwardt, Nov 9, 2004
    #2
    1. Advertising

  3. SMMT wrote:
    > I iterate the collection of listeners and call the listenre method for
    > the particular event.
    > I'm gessing this is not the best way to implement this, so I'm asking
    > for opinions on this.


    It is the common, and usually absolutely sufficient way to implement
    such notifications.

    > I wonder if it is a good practice to create a separated thread so I can
    > move to the next listener without having to wait for the fist to resolve
    > its event handling code.


    You will have to deal with a lot of overhead which is usually not worth
    the trouble. If you want to do it, consider using a design pattern
    called Active Object to isolate the thread execution, and to get a grip
    on the async. event execution.

    > And if so if there are any API to facilitate
    > the work.


    * The existing Swing model implementations already contain the necessary
    code to fire their related events.

    * The old Observer/Observable utilities don't do much good in many
    scenarios.

    * For managing a listener list: javax.swing.event.EventListenerList

    * For AWT event notifications: java.awt.AWTEventMulticaster

    > Also if storing the listeners in a collection is a good ideia
    > (I see no other)


    EventListenerList.

    > and if so, witch implementation is the fastest for
    > iterating porposes...


    Arrays.

    /Thomas
     
    Thomas Weidenfeller, Nov 9, 2004
    #3
  4. SMMT

    SMMT Guest

    Thank you Michael and Thomas for you opinions. It seams I was wrong thinking there is a better way
    doing things , I'll continue using the same code. (Thomas, I was not talking about Swing events ,
    the MVC implementation I talked about is between objects created by my self.)
     
    SMMT, Nov 10, 2004
    #4
  5. SMMT

    Lung Guest

    SMMT <> wrote in message news:<>...
    > Thank you Michael and Thomas for you opinions. It seams I was wrong thinking there is a better way
    > doing things , I'll continue using the same code. (Thomas, I was not talking about Swing events ,
    > the MVC implementation I talked about is between objects created by my self.)


    Hi SMMT, From what I learned in university, this is the way to do it. From
    Understand that's the way to do the "push". And I don't understand why thomas
    said that the old method. Since the way to do things is by push and not by "pull"

    If I were you i would do the same. Since "pull" sucks!!!

    I don't speak very well english, pardon me if I didn't explain myself well.

    I might misunderstood something but your way is the way to push.
     
    Lung, Nov 10, 2004
    #5
    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. stu
    Replies:
    2
    Views:
    417
    Roedy Green
    Apr 25, 2004
  2. dennis
    Replies:
    0
    Views:
    596
    dennis
    May 23, 2006
  3. Jason Cavett

    Listeners and MVC

    Jason Cavett, Nov 10, 2006, in forum: Java
    Replies:
    3
    Views:
    1,903
  4. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    426
    Chris
    Aug 30, 2005
  5. Matt Kruse
    Replies:
    2
    Views:
    186
Loading...

Share This Page