Why don't Listeners use Observer/Observable

T

Timo Nentwig

Hi!

I found that the Listener architecture as it is utilized e.g. by Swing is
manually implemented. Well, isn't Observer/Observable just exactely the use
for this matter?

Timo
 
X

xarax

Timo Nentwig said:
Hi!

I found that the Listener architecture as it is utilized e.g. by Swing is
manually implemented. Well, isn't Observer/Observable just exactely the use
for this matter?

Observable is a class, rather than an interface. The
Swing classes (like JButton, JTextField, etc.) cannot
extend more than one class. Also, Swing uses several
variations of observation depending on the kind of
observable event.

Observer/Observable are generally useless, even if
Observable was an interface, because they don't support
event distinction at the Observer level.

For example, in Swing you can have an ActionListener
and a ListSelectionListener as two distinct observers
that are called for specific kinds of events. A general
purpose Observer doesn't have this level of distinction
(very non-object oriented), thus requiring the Observer
to inspect the event object to see what happened. Swing
can generate zillions of distinct events, and you don't
want to filter out 99% of those in *your* code each time
the event is generated.

The only time that the generic Observer interface is
useful is when your program initiates/schedules an
event to occur. You pass an Observer object to the API
that initiates/schedules the event to occur. When the
event completes, your Observer object is called to notify
your program that the event has completed. No other
"listener" is called, because no other program cares about
the specific event that your program scheduled.

OTOH, the "listener" architecture is intended for
broadcasting event notifications that are of general
interest to many programs.

HTH
 

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,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top