Skip to content

Instantly share code, notes, and snippets.

@rzwitserloot
Created May 7, 2017 13:40
Show Gist options
  • Save rzwitserloot/0d714305268e63dfc4d9581941c53b51 to your computer and use it in GitHub Desktop.
Save rzwitserloot/0d714305268e63dfc4d9581941c53b51 to your computer and use it in GitHub Desktop.
@FunctionalInterface
public interface Action<T extends Base> {
void perform(T input);
}
public class Commands {
private Map<Class<? extends Base>, Action<?>> actions = new HashMap<>();
private <T extends Base> void addAction(Class<T> type, Action<T> action) {
actions.put(type, action);
}
{
addAction(Type1.class, input -> {
// do something with input, which is of type 'Type1', here.
});
addAction(Type2.class, input -> {
// ....
});
}
public <T extends Base> void dispatch(T input) {
Action<T> a = (Action<T>) actions.get(type.getClass());
a.perform(input);
}
}
public class Type1 extends Base {
}
public class Type2 extends Base {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment