Created
March 13, 2009 19:26
-
-
Save seanh/78719 to your computer and use it in GitHub Desktop.
Observer design pattern in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
An implementation of the Observer design pattern in Java. | |
The aims were: | |
* Not make every class that implements Observable deal with implementing the addObserver, | |
removeObserver and notify methods, and maintaining a list of observers. Instead have one class | |
(ConcreteObserver) that implements all this and can be reused. | |
* Not make every class that implements Observable have to extend ConcreteObserver, instead allow | |
them to wrap ConcreteObserver and forward the methods in order to implement Observable. | |
* Not have to do any class casting, particularly in implementations of the Observer.notify | |
method, instead use generics. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package observer; | |
import java.util.Set; | |
import java.util.HashSet; | |
public class ConcreteObservable<T> implements Observable<T> { | |
private Set<Observer<T>> observers = new HashSet<Observer<T>>(); | |
public void addObserver(Observer o) { | |
observers.add(o); | |
} | |
public void removeObserver(Observer o) { | |
observers.remove(o); | |
} | |
public void notify(T arg) { | |
for (Observer<T> o : observers) { | |
o.update(arg); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package observer; | |
/** | |
* A test class that implements observable. | |
* | |
* @author seanh | |
*/ | |
class FooObservable implements Observable<FooObservable> { | |
private ConcreteObservable<FooObservable> | |
obs = new ConcreteObservable<FooObservable>(); | |
private String state = "Initial state."; | |
public void addObserver(Observer<FooObservable> o) { | |
obs.addObserver(o); | |
} | |
public void removeObserver(Observer<FooObservable> o) { | |
obs.removeObserver(o); | |
} | |
public String getState() { | |
return state; | |
} | |
public void setState(String state) { | |
this.state = state; | |
obs.notify(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package observer; | |
/** | |
* A test class that subscribes to a FooObservable. By using an anonymous inner | |
* class as the observer a different method of FooObserver can be called for | |
* each observable that it observes. | |
* | |
* @author seanh | |
*/ | |
class FooObserver { | |
private FooObservable observable; | |
FooObserver(FooObservable observable) { | |
this.observable = observable; | |
observable.addObserver(new Observer<FooObservable>() { | |
public void update(FooObservable observable) { | |
fooUpdate(observable); | |
} | |
}); | |
} | |
public void fooUpdate(FooObservable observable) { | |
System.out.println("This is the update method of FooObserver "+this); | |
System.out.println("The state of my FooObservable "+observable+" is "+observable.getState()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package observer; | |
class Main { | |
public static void main(String[] args) { | |
FooObservable observable = new FooObservable(); | |
FooObserver observer = new FooObserver(observable); | |
observable.setState("New state"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package observer; | |
public interface Observable<T> { | |
public void addObserver(Observer<T> o); | |
public void removeObserver(Observer<T> o); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package observer; | |
public interface Observer<T> { | |
public void update(T arg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment