Interface | Description |
---|---|
Observable<Message> |
Implements this if you want to notify other classes about changes
|
Observer<Message> |
Implement if you want to register with an
Observable |
Class | Description |
---|---|
MultiCaster<Message> |
Send notifications to registered observers about changes to models
|
SwingAwareObserver<Message> |
Implementation of an
Observer that adds calls to notify to the AWT event dispatching
thread. |
Observer
s registers with an
Observable
using Observable#addObserver(Observer)
, and will receive
notifications as a call to Observer#notify(Observable, Object)
.
Typical implementations of Observable
will be delegating to a
MultiCaster
to do the boring observer registration and message
dispatching.
Example of Observable:
public class MyObservable implements ObservableAnd an observer that is notified when MyObservable.doStuff() is called:{ public static class MyEvent { // .. } private MultiCaster<:MyEvent> multiCaster = new MultiCaster<:MyEvent>(this); public void doStuff() { multiCaster.notify(new MyEvent()); } public void addObserver(Observer observer) { multiCaster.addObserver(observer); } public List > getObservers() { return multiCaster.getObservers(); } public void removeObserver(Observer observer) { multiCaster.removeObserver(observer); } }
public class MyObserver implements ObserverExample of usage:{ public void notify(Observable sender, MyEvent message) { System.out.println("Receieved " + message + " from " + sender); } }
MyObservable observable = new MyObservable(); MyObserver observer = new MyObserver(); observable.addObserver(observer); observable.doStuff();
Copyright © 2015–2016 The Apache Software Foundation. All rights reserved.