|
Thursday, 20 January 2005 21:26 |
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Source code/**
* Knows its observers. Any number of Observer objects
* may observe a subject. Provides an interface for attaching
* and detaching Observer objects.
* @role __Subject
*/
public class Subject {
private ArrayList observers = new ArrayList(); public void attach(Observer observer) {
observers.add(observer); }
public void detach(Observer observer) {
int idx = observers.indexOf(observer); if (idx != -1) {
observers.remove(idx); }
}
protected void notifyObservers() {
Iterator it = observers.iterator(); while (it.hasNext()) {
((Observer) it.next()).update(this); }
}
}
/**
* Defines an updating interface for objects that
* should be notified of changes in a subject.
* @role __Observer
*/
public interface Observer {
/**
* Update method.
*/
public void update(Subject subject);}
/**
* Stores state of interest to ConcreteObserver objects.
* Sends a notification to its observers when its state changes.
*/
public class ConcreteSubject extends Subject {
// add your subject's specific stuff here
}
/**
* Implements the Observer updating interface to keep
* its state consistent with the subject's.
*/
public class ConcreteObserver implements Observer {
public void update(Subject subject) {
// put your code here
}
}
|
|
Last Updated ( Friday, 17 June 2005 23:37 )
|
Anyone has a tutorial for developing ...
what type of antifreeze do i use in m...
css lessons - css scrollbar examples ...
SILLY
Now I can stand at the station watchi...