Created
          May 7, 2017 13:40 
        
      - 
      
- 
        Save rzwitserloot/0d714305268e63dfc4d9581941c53b51 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or 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
    
  
  
    
  | @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