Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Event source base class?

Thread view: 
Jeffrey Schwab - 23 Jul 2006 22:56 GMT
Every time I write a class that's an event source, I give it a private
list of event listeners, add/removeListener methods, etc.  What's the
better way to maintain a list of listeners?  I could use
javax.swing.event.EventListenerList, but it has significant
shortcomings:

    - The javadocs do not specify what happens when a listener is
     added or removed multiple times.

    - Proper use of EventListenerList requires a fair amount of
     boilerplate code.

    - It feels a little sketchy to use a Swing utility class when
     I'm not using any other Swing components.

What do you consider best practice, and what are the most reasonable
alternatives?
puzzlecracker - 24 Jul 2006 04:20 GMT
> Every time I write a class that's an event source, I give it a private
> list of event listeners, add/removeListener methods, etc.  What's the
[quoted text clipped - 13 lines]
> What do you consider best practice, and what are the most reasonable
> alternatives?

You shouldn't use swing utility classes if you're not writing gui apps.
For ones, they are tuned for graphics but suffer a poor execution +
space performance in regular applications because of copious use of
synchronization coupled with large buffers.

EvenListenere is fairly common problem with a straightforward,
implementable solution. I suggest you code your own, targeting your
needs.

You should follow this inteface:

interface EventManger{
  add(Listener l);
  remove( Listener l);
  subscribe(Listener l, Even e);
  unSubscribe(Listener l,Even e);
  ...
}

Let me know if this  helps or/and you need more information.

cracker....
Nick Vatamaniuc - 24 Jul 2006 06:34 GMT
Swing's way of doing things is pretty simple. But if you don't want to
have to import it and are worried about other 'behind the scenes' stuff
that might go on,  just whip up your own event model.  If you want to
talk about patterns what you would do is implement the 'observer'
pattern or to make the objects even more decoupled from each other you
would use the 'mediator' pattern.

Here is a short tutorial on the 'observer' pattern with some samples of
code:
http://www.javaworld.com/javaworld/javaqa/2001-05/04-qa-0525-observer.html

Here is a more abstract discussion of it:
http://en.wikipedia.org/wiki/Observer_pattern

Here is my quick explanation of it:
The idea is that there is a 'subject' say a linked-list model, and
there is an observer/ listener, for example some GUI, that wants to
_observe_ the linked list and know when it will be changed so it can
update it's GUI representation.

What  happens is that the 'subject' (linked-list model) will have to
have a list of all the observers/listeners and when it changes, it will
notify them of that change. That is the basic dynamics here. Now, of
course the observers/listeners will have to let the subject know about
themselves. So they will have to _register_ with it to begin with. It
just means that they need to ask the subject to add them to its
observers/listerns list.

If you want to decouple the subject and obserers/listeners as much as
possible you can have an intermediate object that will be in charge of
receiving and dispatching events. Both subjects and observers/listeners
will have to register with it. This is called the 'mediator' pattern.

Here is an example:
http://en.wikipedia.org/wiki/Mediator_pattern

Hope this helps,
Nick Vatamaniuc

> Every time I write a class that's an event source, I give it a private
> list of event listeners, add/removeListener methods, etc.  What's the
[quoted text clipped - 13 lines]
> What do you consider best practice, and what are the most reasonable
> alternatives?
Thomas Hawtin - 24 Jul 2006 12:50 GMT
> Every time I write a class that's an event source, I give it a private
> list of event listeners, add/removeListener methods, etc.  What's the
[quoted text clipped - 4 lines]
>     - The javadocs do not specify what happens when a listener is
>       added or removed multiple times.

All the more reason not to do your own thing!

>     - Proper use of EventListenerList requires a fair amount of
>       boilerplate code.

Not much in comparison to creating a new event. And you only actually
have to do it once per event method. For most purposes
PropertyChangeEvent or ChangeEvent suffice.

The example in the API docs can also be simplified at the cost of a
little performance:

private void fireFooXXX() {
    for (
        FooListener listener :
            listenerList.getListeners(FooListener.class)
    ) {
        listener.fooXXX(new FooEvent());
    }
}

(getListeners reverses the returned array...)

>     - It feels a little sketchy to use a Swing utility class when
>       I'm not using any other Swing components.

It's only there by historical accident. Java's packages are all over the
shop. Think of it as being in java.beans.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.