Les Morgan wrote:
I don't think this will even compile as is since getSource returns an Object. You'd have to cast it to an interface or base class first then call the method.
Another option would be to use a Map<Button, Handler> where handler is again an interface or base class. If you're using
Java 8, handler can be a functional interface which then opens up the opportunity to use lambdas or method references. This way, you'd simply pass e.getSource() to map.get() to lookup the appropriate handler. Then you just invoke whatever handler you get back. The nice thing about this too is that you can adhere to the
Open-Closed Principle by injecting the Map.