具有多态类值的ImmutableMap的类型说明符?

时间:2017-03-27 22:19:20

标签: java oop guava factory factory-pattern

我尝试使用ImmutableMap创建类类型的命名字典:

private static final Map<String, Class<FruitFactory>> REGISTERED_FACTORIES =
    ImmutableMap.of(
        "AppleOrchard", AppleFactory.class,
        "OrangeGrove",  OrangeFactory.class
    );

FruitFactory是一个接口:

public interface FruitFactory {
    Fruit createFruit();
}

AppleFactoryOrangeFactory是其实现:

public class AppleFactory implements FruitFactory {
    @Override
    public Fruit createFruit() {
        return new Apple();
    }
}

public class OrangeFactory implements FruitFactory {
    @Override
    public Fruit createFruit() {
        return new Orange();
    }
}

问题是不能编译的。我从ImmutableMap.of(电话中收到以下构建错误:

 error: incompatible types: inference variable V has incompatible bounds

equality constraints: Class<FruitFactory>
lower bounds: Class<AppleFactory>,Class<OrangeFactory>

最终我认为解决方案在于我的初始地图声明Map<String, CRZNSS> REGISTERED_FACTORIES的一些狂野泛型语法。我尝试了几次?Tfoo extends bar等等的迭代,但没有任何效果。

有什么想法吗?

0 个答案:

没有答案