gpt4 book ai didi

泛型类的 Java 工厂

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:51 26 4
gpt4 key购买 nike

这个问题类似于Factory and generics ,并且可能有相同的答案,但它是不同的。我有一个通用基类,它将由完全独立的 JAR 中的类进行扩展。所述 JAR 应该能够在不更改任何其他代码的情况下放入和替换(代码根据命令执行)。

基类:

public abstract class ThingBase<A> {

public abstract A doThing();

public abstract Thing getThingType();

// This might be the wrong way to do this, and I elaborate below.
public final SortedMap<Long, A> getCollectionContainer() {
return new TreeMap<Long, A>();
}

}

我有一个枚举,我将为每个条目创建不同的事物扩展。

public enum Thing { T1, T2, T3 }

创建此类对象的工厂:

public class ThingFactory {
private static final Map<Thing, Class<? extends ThingInterface<?>>> thingMap;

static{//populate map by finding all extensions of base class}

public static ThingBase<?> getThing(Thing thing) {
try {
Class<? extends ThingBase<?>> thingImpl = thingMap.get(thing);
if (thingImpl != null) {
return thingImpl.newInstance();
} else {
return null;
}
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
return null;
}
}
}

我的问题是下面代码中未经检查的原始类型警告(注意不是上面,上面的代码编译得很好)。在以下用法中,代码成功执行,但我收到未经检查的类型警告。

public class ThingAccumulator {
// Raw Type
private final ThingBase myThing;

// Raw Type
private SortedMap myMap;

public ThingAccumulator(Thing thing) {
myThing = ThingFactory.getThing(thing);
myMap = myThing.getCollectionData();
//Type safety: The method put(Object, Object) belongs to the raw type Map. References to generic type Map<K,V> should be parameterized
myMap.put(System.currentTimeMillis(), myThing.doThing());
}
}

放置 < ? > ThingBase 和 SortedMap 字段上的 < Long, ?> 提示 ? 不同:

Map 类型中的 put(Long, capture#17-of ?) 方法不适用于参数 (long, capture#18-of ?)

现在这里的主要问题似乎是编译器无法假设?和 ?是相同的,这是公平的,但到目前为止我想出的唯一解决方案是将类型放入持有类中,这不是一个选项,因为只有实现知道数据类型:/

有人知道如何消除这些警告吗?有更好的方法吗?

谢谢

SDY

最佳答案

您可以将泛型类型参数添加到 ThingAccumulator ,使用它来移动事物,以便 Java 提示的唯一代码是您可以证明确实是类型安全的简单行,然后抑制该警告,因为您知道这不是问题。

ThingAccumulator 提供类型参数让它的字段在一起使用时进行类型检查。你可以提出“但是类型是什么?”的问题。在构造函数之外,采用 ThingBase<A>实例作为参数而不是 Thing枚举值。然后,静态工厂方法可以处理对象构造:

public class ThingAccumulator<A> {
private final ThingBase<A> myThing;

private SortedMap<Long, A> myMap;

public static ThingAccumulator<?> create(Thing thing) {
return new ThingAccumulator(ThingFactory.getThing(thing));
}

protected ThingAccumulator(ThingBase<A> thing) {
myThing = thing;
myMap = myThing.getCollectionData();
myMap.put(System.currentTimeMillis(), myThing.doThing());
}
}

我希望 Java 喜欢 create()功能。逻辑可能是这样的:

  • ThingFactory.getThing()返回 ThingBase<?> 。这就像 ThingBase<X>对于一些未知的X 。我们不知道它并不意味着它不存在。
  • new ThingAccumulator()可以拍ThingBase<X> ,对于任何 X ,并返回 ThingAccumulator<X> .
  • 现在我们有一个ThingAccumulator<?> — 具有未知类型参数的实例。类型安全尚未被破坏。

但是,Java 不喜欢它(至少在我的 Java 1.7.0 版本中)。但如果我们喜欢它,我们可以抑制警告:

    @SuppressWarnings("unchecked")
public static ThingAccumulator<?> create(Thing thing) {
return new ThingAccumulator(ThingFactory.getThing(thing));
}

并且因为所有其余代码(到目前为止......)都是类型安全的,所以不再有警告。

关于泛型类的 Java 工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24318024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com