[Biojava-l] Immutable objects and ChangeListeners

Matthew Pocock mrp@sanger.ac.uk
Tue, 09 Jan 2001 11:00:48 +0000


Hi. As the architect of this particular can of worms, I guess I should speak
up here. In this case, FastaSearchResult will inherit from Annotatable that
in turn inherits from Changeable. You wish the state in FastaSearchResult
instances to be immutable. The Annotatable interface both defines the
getAnnotation method and also an inner class for notifying listeners to the
Annotatable that the Annotation has altered.

The correct  least resistance implementation of a FastaSearchResult would
either inherit from AbstractChangeable or to use a ChangeSupport and
explicitly delegate add/remove listener methods to it (as AbstractChangeable
does internaly). Then, during construction, once the Annotation property is
set, it should register an instance of AnnotationForwarder with the
Annotation, and store a reference to that in the SimpleFastaSearchResult
instance (as a private field?) to prevent the listener being garbage
collected.

Hope this makes things clearer. I will fix the Annotatable documentation to
make this explicit.

Matthew

Keith James wrote:

> I have a question re. objects designed to be immutable under all
> circumstances and yet implementing Changeable.
>
> e.g. I wanted FastaSearchResult to be immutable but also to implement
> Annotatable (the search header contains a lot of fairly loosely
> formatted, but worthwhile data). This means that it has to implement
> add/removeChangeListener.
>
> I suppose I could implement the methods as stubs which do nothing.
> I've seen this elsewhere in Biojava, I think. As the methods return
> nothing and can't throw exceptions I guess this should work. However
> is it good that someone who uses these methods (and is therefore
> expecting the object to change) should be able to add/remove a
> Listener? Or think that they had, when in fact nothing had happened.
>
> As the methods are not allowed to throw exceptions, how can you
> politely say "Are you sure you want to listen for changes - this
> object is immutable" ?
>
> --
>
> -= Keith James - kdj@sanger.ac.uk - http://www.sanger.ac.uk/Users/kdj =-
> The Sanger Centre, Wellcome Trust Genome Campus, Hinxton, Cambs CB10 1SA
> _______________________________________________
> Biojava-l mailing list  -  Biojava-l@biojava.org
> http://biojava.org/mailman/listinfo/biojava-l