使用泛型链接任务 [英] Chaining tasks using generics
问题描述
首先,我设计了一个非常简单的界面来定义任务:
public interface任务< U,V> {
U执行(V输入);
其中 V
是输入类型和 U
任务的输出类型。
我想创建的是 Chain
类将链接任务
。
列表,我想执行: Task1< TypeA,TypeB> - > Task2< TypeB,TypeC> - > Task3< TypeC,TypeD>
这个 Chain
c $ c $>任务< TypeA,TypeD> 。
所以我编写了这段代码,它不能编译:
public class Chain< U,V>实现Task< U,V> {
列表<任务<?,?>>任务列表;
$ b $ public Chain(){
taskList = new LinkedList< Task<?,?>>();
}
@Override
public U execute(V input){
V currentInput = input;
U output = null;
for(Task<?,?> task:taskList){
output = task.execute(currentInput);
//编译错误,因为currentInput的类型为V
//并且输出的类型为U
currentInput = output; //编译错误以及
}
返回输出;
}
//在列表中添加和删除任务的其他方法
}
我明白为什么它不能编译,但我不知道如何实现一些可行的方法并解决我的问题。
有没有人有没有遇到这种问题?
亲切的问候,
本
<您的
Task
类与 Guava class Function
。你可以使用它而不是定义你自己的。 使用 Function
的好处是你可以使用 函数
类,其中之一是 compose()
:
函数< String,MyObject1> stringToMyObject1 = ...;
函数< MyObject1,AnotherObject> myObject1ToAnotherObject = ...;
函数< String,AnotherObject> stringtoAnotherObject = Functions.compose(myObject1ToAnotherObject,stringToMyObject1);
通过反复调用 compose()
$ b即使你因为某种原因不想使用番石榴,你也可以从这种方法中获得灵感:将已知的任何两种功能/任务类型参数用易于计算的类型参数创建一个新的函数/任务。
I am trying to create an API for chaining tasks.
First, I designed a very simple interface defining a task:
public interface Task<U, V> {
U execute(V input);
}
Where V
is the input type and U
the output type of the task.
What I whish to create is a Chain
class that would chain a list of Tasks
.
For instance, I would like to execute: Task1<TypeA, TypeB> -> Task2<TypeB, TypeC> -> Task3<TypeC, TypeD>
This Chain
class would thus be a Task<TypeA, TypeD>
.
So I wrote this code, which does not compile :
public class Chain<U, V> implements Task<U, V> {
List<Task<?, ?>> taskList;
public Chain() {
taskList = new LinkedList<Task<?, ?>>();
}
@Override
public U execute(V input) {
V currentInput = input;
U output = null;
for (Task<?, ?> task : taskList) {
output = task.execute(currentInput);
// Compile error because currentInput is of type V
// and output of type U
currentInput = output; // Compile error as well
}
return output;
}
// Other methods to add and remove tasks in the list
}
I do understand why it cannot compile but I have no idea how I could implement something that works and answers my problem.
Has anyone ever faced this kind of problem?
Kind regards,
Ben
Your Task
class is pretty much the same as the Guava class Function
. You could use that instead of defining your own one.
An advantage of using Function
would be that you could use the helper methods in the Functions
class, one of which is compose()
:
Function<String,MyObject1> stringToMyObject1 = ...;
Function<MyObject1, AnotherObject> myObject1ToAnotherObject = ...;
Function<String,AnotherObject> stringtoAnotherObject = Functions.compose(myObject1ToAnotherObject, stringToMyObject1);
Deeper chaining can be achieved by repeated calls to compose()
.
Even if you don't want to use Guava for some reason, you can take inspiration from this approach: chaining any two functions/task with known type arguments creates a new function/task with easily-calculated type arguments.
这篇关于使用泛型链接任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!