Collection classes in Java: Why arent they Observable?

S

shafik

Hello folks,

Im not sure if this was pondered before or not. But why wouldnt you
have the Collection classes be observable, i.e. you can attach
listeners to them that listen to a state change in the collection (add,
remove, etc). Wouldnt that make sense?


--Shafik
 
J

John C. Bollinger

Im not sure if this was pondered before or not. But why wouldnt you
have the Collection classes be observable, i.e. you can attach
listeners to them that listen to a state change in the collection (add,
remove, etc). Wouldnt that make sense?

That might be a useful thing to do under certain special circumstances,
in which case it is relatively easy to roll your own wrapper around
existing collections to make it happen. In general, however, this kind
of behavior is rarely desired, and it does involve some overhead even
when there are no observers registered.

Perhaps a better explanation OO-wise is that being observable is not
central to being a Collection. As such, it would *not* have made sense
to make all Collections Observable, no matter how convenient you might
find it at the moment.
 
I

Ian Shef

Hello folks,

Im not sure if this was pondered before or not. But why wouldnt you
have the Collection classes be observable, i.e. you can attach
listeners to them that listen to a state change in the collection (add,
remove, etc). Wouldnt that make sense?


--Shafik

Unfortunately, Observable is a class, not an interface.
Perhaps a wrapper could be set up, similar to the way that List
synchronization is handled:

List list = Collections.synchronizedList(new ArrayList(...));

.... or you could provide extensions of the Collections classes that contain
an Observable and do the right thing.
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top