在Java中对抽象泛型类变量进行方法调用



老实说,我很难解决这个问题。假设我们有一个基类:

public abstract class Work {
}

以及两个实现:

public class SimpleWork extends Work {
}
public class HardWork extends Work {
}

现在我想要一个具有通用工作类型的抽象工作执行器:

public abstract class AbstractWorkExecuter<TWork extends Work> {
public abstract WorkResult executeWork(TWork work);
}

执行器的两个实现:

public class SimpleWorkExecuter extends AbstractWorkExecuter<SimpleWork> {

public abstract WorkResult executeWork(SimpleWork work);
}
public class HardWorkExecuter extends AbstractWorkExecuter<HarkWork> {

public abstract WorkResult executeWork(HardWork work);
}

然后我需要创建一些工厂来实现我需要的执行器:

public class WorkExecutorFactory {
public static AbstractWorkExecuter<?> create(String type) {
if (type.equals("simple")) {
return new SimpleWorkExecuter();
} else {
return new HardWorkExecuter();
}
}
}

现在我想使用所有这些:

public static void main (String args []) {
Work work = new SimpleWork();
//This line works fine
AbstractWorkExecuter<?> workExecuter = WorkExecutorFactory.create("simple");
//There is the problem
workExecuter.executeWork(work);     
}

Eclipse中的最后一行显示了这样的异常:

The method executeWork(capture#2-of ?) in the type AbstractWorkExecuter<capture#2-of ?> is not applicable for the arguments (Work)

那么,我该怎么做才能让它发挥作用呢?我想避免使用原始类型。需要一提的是,我不知道我可能需要什么实现,而真正的项目有比这个例子中的两个更多的实现。提前谢谢。

@Aleks,您的方法是正确的,通常都是关于依赖注入的,非常扩展主题。

您的问题根本不需要定义要使用的执行器。只有使用类型和反射Java才能匹配、实例化和运行每项工作。

public class Test {
// the core team define some basic names
interface Work {}
interface WorkExecutor<T extends Work> {
default void executeWork(T work) {
System.out.printf("%s run %s%n", this.getClass().getName(), work.getClass().getName());
}
}
// some dev team create simple works
static class SimpleWork implements Work {}
// some dev team create hard works
static class HardWork implements Work {}
// some dev team create simple executors
static class SimpleWorkExecutor implements WorkExecutor<SimpleWork> { }
// some dev team create hard executors
static class HardWorkExecutor implements WorkExecutor<HardWork> { }
// (extra requirement) some dev team create aditional (sidecars) hard executors
static class SideCarHardWorkExecutor implements WorkExecutor<HardWork> { }

@SneakyThrows
public static void main(String... args) {
// somewhere somebody define works
List<Work> works = asList(new SimpleWork(), new HardWork());
// for each work to do
for(Work w: works) {
final String validExecutorType = WorkExecutor.class.getName() + "<" + w.getClass().getName() + ">";
// find all available workers, simply ensure the jar, class, ... are loaded (websphere, jboss, java -jar...)
// NOTE: reflection could be hard, use a good reflection dependency injection library!
for (Class<?> clazz : Test.class.getDeclaredClasses())
if(Arrays.stream(clazz.getGenericInterfaces()).map(Type::getTypeName).anyMatch(validExecutorType::equals)) {
((WorkExecutor<?>) clazz.newInstance()).executeWork(cast(w));
// you can break or continue doing the work with other available worker executors
}
}
}
private static <T extends Work> T cast(Work w) {
return (T) w;
}
}

带输出

Test$SimpleWorkExecutor run Test$SimpleWork
Test$SideCarHardWorkExecutor run Test$HardWork
Test$HardWorkExecutor run Test$HardWork

只包含/放置类,匹配将是自动的。

注意:foor循环体是所使用的DI库的特定部分,请在实际项目中使用之前了解并练习。

不能用字符串来实现这一点。没有足够的静态类型信息来确保编译时的类型正确性。

创建工厂,返回所需类型的实例。例如:

class WorkExecutorFactory<T> {
private final Supplier<T> supplier;
private WorkExecutorFactory(Supplier<T> supplier) {
this.supplier = supplier;
}
T create() { return supplier.get(); }
static final WorkExecutorFactory<SimpleWork> SIMPLE_FACTORY = new WorkExecutorFactory<>(SimpleWorkExecuter::new);
}

然后可以使用SIMPLE_FACTORY.create()(它携带它创建的东西的类型信息(而不是WorkExecutorFactory.create("simple")

最新更新