I have a common interface Command
:
public interface Command<T> {
public void execute(T value);
}
And some implementations:
public class ChangeName implements Command<String>{
public void execute(String value) {...}
}
public class SetTimeout implements Command<Integer>{
public void execute(Integer value) {...}
}
I need to Map
bind command names to a specific object Command
:
Map<String, Command> commands = new HashMap<>();
...
commands.put("changeName", new ChangeName());
Obviously, I get warnings rawtypes
when declaring Map
. If I use a question mark, I get a compilation error:
Map<String, Command<?>> commands = new HashMap<>();
...
commands.get("changeName").execute("Foo");
Executing a method (capture # 2-of?) In a type command is not applicable for arguments (String)
I know that you cannot have a varied heterogeneous container with a non-recoverable type ( Item 29 in Effective Java), but what is the best approach to solve this problem?
source
share