Bastian Helfert wrote:
I can think of a number of uses for a weakly referencing Set, too. In fact, one example involves a class already in collections that could be modified to use such a Set. The UniquePredicate class is a Predicate implementation whose evaluate() method returns true the first time it encounters any given input object, and returns false whenever the same object is encountered again. The current implementation retains each evaluated object in an internal Set. The evaluate method returns true if an input object is not in the set (the object is added to the set, in this case), or false if the object was already in the set."Jakarta Commons Developers List" <[email protected]> schrieb am 24.12.04 11:09:48:
Well, here's an example, of the top of my head. A weakly referencing list could be useful for implementing the observer pattern in a situation where you don't want the listeners to be strongly referenced by the event source. If the event source uses such a list to hold its listeners, then as long as the listeners remain strongly referenced, they will continue to receive events from the source. There will be no need to explicitly unregister the listeners, as once they become weakly referenced, then they will effectively be removed from the event listener list automatically.
Chris
Thank you Chris, this is exactly what I was thinking of :). With such a List, it is possible to protect developers against creating memory leaks unintentionally, when they forget to invoke the accordant removeListener(...) method (assuming the list is strongly referenced by a long-living object). I think there is an Antipattern called "Lapsed Listeners Leak" that describes this problem.
________________________________________________________________
If the internal set instance was a weakly referencing Set, then the UniquePredicate wouldn't prevent any of the evaluated objects from being GCed. Of course, this does change the semantics of the UniquePredicate slightly. The current implementation determines if an object is being reevaluated on the basis of object equality, whereas weak references are (by their very nature) reference based. For this reason, I'm not suggesting that UniquePredicate actually be changed, but this does serve as an example of one potential use of a weakly referencing Set.
On the other hand, regarding Stephen's original comment, I've also struggled to come up with any non-contrived use cases for softly referencing Lists and Sets. I'm sure there must be some, though...
Chris
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
