2

我正在尝试使用泛型实现命令模式的非常简单的实现

public abstract class Command { 
}


public interface CommandHandler<H extends Command> {

    boolean isActive();     
    void execute( H command );

}


public class CommandExecutionServiceImpl implements CommandExecutionService {

    private Map< Class<Command>,CommandHandler<Command>> commandMap; 


    public CommandExecutionServiceImpl(){       
        commandMap = new HashMap<Class<Command>, CommandHandler<Command>>();        
    }

    @Override
    public void executeCommand(Command command) {

        CommandHandler<Command> handler = commandMap.get(command.getClass());
        handler.execute(command);       

    }

    @Override
    public boolean isActive(Command command) {
        return false;
    }

    @Override
    public <H extends Command> void addCommandHandler(Class<H> commandClass, CommandHandler<H> handler) {       

        commandMap.put( commandClass, handler );

    }

编译器失败

编译失败CommandExecutionServiceImpl.java:[36,12] put(java.lang.Class,CommandHandler) in java.util.Map,CommandHandler> 不能应用于(java.lang.Class,CommandHandler)

我不明白为什么编译器无法推断 commandMap.put(commandClass, handler); 的类型

任何帮助,将不胜感激。

4

2 回答 2

3

看起来您想要地图的键和值之间的通用关系。如果支持,它可能如下所示:

private <H extends Command> Map<Class<H>, CommandHandler<H>> commandMap;

但这显然是不允许的。一种解决方法是使用 Josh Bloch 的Typesafe Heterogeneous Container模式:

private Map<Class<? extends Command>, CommandHandler<? extends Command>> commandMap; 

@Override
public <H extends Command> void executeCommand(H command) {

    // addCommandHandler guarantees the safety of this unchecked cast
    @SuppressWarnings("unchecked")
    CommandHandler<H> handler = (CommandHandler<H>)commandMap.get(command.getClass());
    handler.execute(command);
}
于 2013-09-27T02:30:11.627 回答
1

您的地图已声明:

Map< Class<Command>,CommandHandler<Command>>

但是您尝试输入以下类型的值:

Class<H> commandClass, CommandHandler<H> handler

在哪里<H extends Command>

也许您应该将泛型类型添加到类型的 CommandExecutionService<T extends Command>并将映射声明为:

Map< Class<T>, CommandHandler<T>>

和方法:

public void addCommandHandler (Class<T> commandClass, CommandHandler<T> handler) { ... }
于 2013-09-27T01:31:05.493 回答