Skip to content

Instantly share code, notes, and snippets.

@mindwing
Created June 8, 2016 05:22
Show Gist options
  • Save mindwing/e7bab6ce6838cf165087845e6867d673 to your computer and use it in GitHub Desktop.
Save mindwing/e7bab6ce6838cf165087845e6867d673 to your computer and use it in GitHub Desktop.
Agera Explained - 08.Incrementally Agerifying legacy code
public final class MyListenable extends SomeBaseClass implements Observable {
private final UpdateDispatcher updateDispatcher;
public MyListenable() {
// Original constructor code here...
updateDispatcher = Observables.updateDispatcher(new Bridge());
}
// Original class body here... including:
public void addListener(Listener listener) { … }
public void removeListener(Listener listener) { … }
@Override
public void addUpdatable(Updatable updatable) {
updateDispatcher.addUpdatable(updatable);
}
@Override
public void removeUpdatable(Updatable updatable) {
updateDispatcher.removeUpdatable(updatable);
}
private final class Bridge implements ActivationHandler, Listener {
@Override
public void observableActivated(UpdateDispatcher caller) {
addListener(this);
}
@Override
public void observableDeactivated(UpdateDispatcher caller) {
removeListener(this);
}
@Override
public void onEvent() { // Listener implementation
updateDispatcher.update();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment