kaapo
Class Observable

java.lang.Object
  extended by kaapo.Observable
Direct Known Subclasses:
DiagramComponentWrapper, Project, ProjectComponent, ProjectManager

public abstract class Observable
extends Object

This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed. Subclassing classes must take care to call setChanged and notifyObserver method whenever their state changes.

This is basically the same as Java's Observable class, except that the observer list is marked as transient, meaning that it wont be serialized by object serialization. That way a project can be saved without any references to application GUI or some other classes.

The notifyObservers methods won't do anything unless the subject's state is set to changed by calling it's setChanged method.

It may be good idea to use Enums as notifyObserver method's message parameter and then test message objects equality in update method.

Author:
paltamaa
See Also:
Observer, Observable, Observer design pattern

Constructor Summary
Observable()
          Creates new Observable object with no observers.
 
Method Summary
 void addObserver(Observer observer)
          Adds new observer to this subject.
 void notifyObservers()
          Notifies all observers by calling their update method with null message.
 void notifyObservers(Object message)
          Notifies all observers by calling their update method with given message.
 void removeAllObservers()
          Removes all observers from this subject.
 void removeObserver(Observer observer)
          Removes an observer from this subject.
 void setChanged()
          Sets this observers state to changed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Observable

public Observable()
Creates new Observable object with no observers.

Method Detail

addObserver

public void addObserver(Observer observer)
Adds new observer to this subject.

Parameters:
observer - the observer to add

removeObserver

public void removeObserver(Observer observer)
Removes an observer from this subject.

Parameters:
observer - the observer to remove

removeAllObservers

public void removeAllObservers()
Removes all observers from this subject.


setChanged

public void setChanged()
Sets this observers state to changed.


notifyObservers

public void notifyObservers()
Notifies all observers by calling their update method with null message.

It's safe to modify the subject's observer list from update method. It won't affect the current notify round though, but the next.


notifyObservers

public void notifyObservers(Object message)
Notifies all observers by calling their update method with given message.

It's safe to modify the subject's observer list from update method. It won't affect the current notify round though, but the next.

Parameters:
message - Message to send