Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.observer
Interface Observable<O>

Type Parameters:
O - The type of observers associated with this observable.
All Known Subinterfaces:
AspectObservable<O,A>, AspectObservableSequence<O,A,E>, ObservableSequence<O,A,E>
All Known Implementing Classes:
AnnotatedObserversSequence, AnnotatedObserversSequenceDecorator, DateSequence, ObserverManager, SequenceObserversSequence, SequenceObserversSequenceDecorator

@Participant(value="Subject")
public interface Observable<O>

An observable object is an object that has a collection of associated observers to be notified in case the object changes its internal state, or aspect.

This interface only supplies the logic for adding, removing, and fetching associated observers, but no notification method to notify the observers.

Instead, the type of observers is supplied as the type parameter O and implementing classes must supply the notification mechanism. This allows for greater flexibility, for example asynchronous notification, as well as the use of different methods.

Implementation notes:
This is essentially an application of the Composite pattern.

It is not allowed to implement the same generic interface more than once by a given type because erasure will cause the signatures to conflict. Hence, an observable type should instead use a broader type of O to allow different unique observer types. Internally, composition could then be used via stand-alone Observable implementations, such as the ObserverManager class, each handling a specific type of observers.

Author:
Gunni Rode / rode.dk
See Also:
AspectObservable

Method Summary
 boolean addObserver(O observer)
          Adds the observer supplied as observer to this observable, if not already.
 Collection<O> getObservers()
          Returns the observers currently associated with this observable sequence.
 boolean removeObserver(Object observer)
          Removes the observer supplied as observer from this observable, if already added.
 

Method Detail

addObserver

boolean addObserver(@Participant(value="ConcreteObserver")
                    O observer)
Adds the observer supplied as observer to this observable, if not already.

Parameters:
observer - The observer to add; cannot be null.
Returns:
True if observer was added, false if not.
Throws:
NullPointerException - If observer is null.
IllegalArgumentException - If observer is not a valid observer to add to this observable (optional).
See Also:
removeObserver(Object)

getObservers

Collection<O> getObservers()
Returns the observers currently associated with this observable sequence.

No specific order is maintained.

Modifying the returned collection will not affect this observable.

Returns:
The collection of observers; never null, but can be empty.

removeObserver

boolean removeObserver(Object observer)
Removes the observer supplied as observer from this observable, if already added.

Trying to remove an observer that is not associated with this observable has no effect.

Parameters:
observer - The observer to remove; cannot be null.
Returns:
True if observer was removed, false if not.
Throws:
NullPointerException - If observer is null.
See Also:
addObserver(Object)

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.