我用command
模式类似如下:
public interface Command<T extends EObject>
{
void runCommand(T classToMap, String fieldForMapping);
}
正如有人可能会注意到,我与Eclipse Modelling Framework
工作,但这对这个问题无关紧要。
在另一个类中,我有一个HashMap作为mappingDirectory,该键返回另一个Map。
private final static HashMap<String, LinkedHashMap<String, Command>> mappingDirectory = new HashMap<String, LinkedHashMap<String, Command>>();
对此mappingDirectory我添加各种地图看起来像这样:
private final static LinkedHashMap<String, Command> classOneMappings = new LinkedHashMap<String, Command>();
private final static LinkedHashMap<String, Command> classTwoMappings = new LinkedHashMap<String, Command>();
...等等。这些地图充满了密钥和所提到的命令接口:
classOneMappings.put("someKey", new Command<ClassOne>()
{
@Override
public void runCommand(ClassOne classToMap, String fieldForMapping)
{
classToMap.setName(fieldForMapping);
};
});
最后我有很多不同类型的实现命令接口。那么我怎样才能避免使用Command
作为mappingDirectory
的原始类型和包含的地图?
我认为这将有可能得到这个工作是这样的:
HashMap<String, LinkedHashMap<String, Command<? extends EObject>>> mappingDirectory = new HashMap<String, LinkedHashMap<String, Command<? extends EObject>>>();
,并与他们的具体类型它们所有EObjects
声明其他地图:
private final static LinkedHashMap<String, Command<ClassOne>> classOneMappings = new LinkedHashMap<String, Command<ClassOne>>();
但可悲的是把他们进入mappingDirectory
将导致编译错误。
也许我只是俯瞰一些东西,所以任何帮助将不胜感激!
感谢您的时间:)这可悲的结果是与Rohits建议相同的问题:编译错误,抱怨说我不能用EObject调用runCommand方法。有关更多详情,请参阅下面的Rohits建议的第一条评论。 – Ben
好吧,在这种情况下唯一的办法是将变量'mappingDirectory'声明为'Map>'。我会编辑我的答案。希望这可以帮助。 –
Seelenvirtuose
好吧,那比以前好多了。我想这不能在不改变结构本身的情况下进一步加强。谢谢! :) – Ben