Duplication in events mechanisms in java?

T

Tomer Ben-David

Hello

I am going to write a generic event system to my application.

So did a little bit of investigation of ready made events mechanisms
in java and found these:

Section 1 :

java.util.Observable
java.util.Observer

They seem OK to me... Then I moved on to see some API of events in
servlets and noticed these guys:

Section 2 :

javax.servlet.http.HttpSessionListener
java.util.EventListener


My question is :
Why isnt there any connection between the classes/interfaces in
section2 to section1 isnt there actually a code duplication?!

(i.e. Isnt the listener in java.util.EventListener actually an
Observer?
Why do they have this duplicity?)

What should I use in case I want to have a generic events framework in
my system?

Thanks in advance,

Tomer

Thanks in advance

Tomer
 
H

Harald Hein

Tomer Ben-David said:
Section 1 :

java.util.Observable
java.util.Observer

They seem OK to me...

No, they are not ok for non trivial event systems, because:

Main reason: Observable is a class. So you have to derive all event
sources from Observable. This is to much of a restriction for a lot
of real world systems.

Minor reason: Only one type of event (the update() event) is possible.
You have to discriminate between different events and event reasons by
looking on the second generic argument to update(), which can become
tedious.
My question is :
Why isnt there any connection between the classes/interfaces in
section2 to section1 isnt there actually a code duplication?!

See above.
What should I use in case I want to have a generic events
framework in my system?

You check your requirements. Depending on your requirements you use one
of the existing mechanisms, write your own, or e.g. pay some $$$ for a
message bus library.

The better you can define your requirements, the less generic will be
the systems that you need, and the simpler it can be.
 
T

Tomer Ben-David

java.util.Observable
No, they are not ok for non trivial event systems, because:

I'm not plaining on using them directly I'm planning on building an
architecture that is going to be based on them (or on the other kind
of event architecture : java.util.EventListener)) that means that I
can use them for non trivial systems, since I'm only building my
infrastructure upon them in a way where i wont see their
disadvantages.

So that means that regarding the main reason : Main reason: Observable
is a class and the minor reason I was going to overcome them (easily)
by building my own infrastracture upon the observer/observabale ...

So I still have my question, I'm looking for the base, the common
ground of events system to build my architecture upon, I see two such
systems in java

the observer/observabale & EventListener , both these infrastructures
are solving a case where one part of the system needs to notify/send
events to other parts of the system, why dont they have a common
ground? (perhaps in a way of common ancestor interfaces) When
building an events sytem ARCHITECTURE what should it be BASED upon?
the observer/observabale or EventListener?

Thanks

Tomer
 
D

Dale King

Tomer Ben-David said:
I'm not plaining on using them directly I'm planning on building an
architecture that is going to be based on them (or on the other kind
of event architecture : java.util.EventListener)) that means that I
can use them for non trivial systems, since I'm only building my
infrastructure upon them in a way where i wont see their
disadvantages.

So that means that regarding the main reason : Main reason: Observable
is a class and the minor reason I was going to overcome them (easily)
by building my own infrastracture upon the observer/observabale ...

For more discussion on this see this article which talks about the event
generator idiom and why Observer/Observable are to be avoided.

http://www.javaworld.com/jw-09-1998/jw-09-techniques.html
So I still have my question, I'm looking for the base, the common
ground of events system to build my architecture upon, I see two such
systems in java

the observer/observabale & EventListener , both these infrastructures
are solving a case where one part of the system needs to notify/send
events to other parts of the system, why dont they have a common
ground? (perhaps in a way of common ancestor interfaces)

Basically, observer/observable are simply too generic.
When
building an events sytem ARCHITECTURE what should it be BASED upon?
the observer/observabale or EventListener?

The event generator idiom as described in the article above. EventListener
can be used in that idiom, but is an empty interface, so by itself does not
actually dictate the solution.
 
T

Tomer Ben-David

Which got answered.

Was not.
Thanks Harald.

Anybody else has a view on this subject?

Thanks in advance,

Tomer
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top