Skip to content

Instantly share code, notes, and snippets.

@joostlawerman
Last active December 13, 2016 17:29
Show Gist options
  • Save joostlawerman/014fa56db270e737f2318ba83a99351f to your computer and use it in GitHub Desktop.
Save joostlawerman/014fa56db270e737f2318ba83a99351f to your computer and use it in GitHub Desktop.
Simple Java observer wrapper
import java.util.Observable;
public class SubscribableObserver<valueType> extends Observable {
protected valueType value;
public SubscribableObserver(valueType value) {
this.value = value;
}
public void update(valueType value) {
this.value = value;
}
public valueType getValue(valueType value) {
return value;
}
interface Subscriber<valueType> {
void operation(valueType value);
}
public void subscribe(Subscriber<valueType> subscriber) {
this.addObserver((Observable o, Object args) -> {
subscriber.operation(this.value);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment