Listeners and Events

S

SMMT

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.
 
M

Michael Borgwardt

SMMT said:
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).
 
T

Thomas Weidenfeller

SMMT said:
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
 
S

SMMT

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.)
 
L

Lung

SMMT said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top