I want to implement some kind of Command Pattern in Java. I want to have a structure like Map<String commandkey, Function()>
. So I have an object (Map
, HashMap
, LinkedHashMap
or whatever associative...) where keys are string commands and values are functions which i want to call by the key. These functions have to be heterogeneous in the sense the can have different return values, number of parameters, names (different signatures). In C++ e.g. I can create a Map of function pointers or functors via boost::function.
So can someone name all the ways of implementing such an idiom (or a pattern if we look at it in broad sense) in Java. I know two ways:
- Reflection (minus: slow and very ugly)
- Using an interface and anonymous classes (minus: functions must have the same signature)
Detail explanation, links to articles and so on will be very helpful.