design/process 在 Java 8 中对不同方法的有序调用的最佳方法是什么
What is the best way to design/process in Java 8 ordered calls on different methods
我的问题是关于设计的
我正在尝试根据不同的方法设计 order/sequential 调用。
假设我的 Class :
public class Foo {
public Object method1(String a){
// impl1..
}
public Object method2(List<Object> list){
// impl2..
}
public Object method3(Map<String,String> map, Object otherStuff){
// impl3..
}
// other different methods ....
}
我想遵循这个概念
http://qrman.github.io/posts/2017/02/09/pipeline-pattern-plumber-quest
但我的不同之处在于,我使用唯一的 1 class 和多种方法,如果我有不同的服务,我会为每个服务创建新的 class 接口实现,如 [=38] =] ,但我的目的是在 1 class 中创建顺序方法列表,它将迭代并执行它们...
我在想一些基于java 8
的方法参考
就像这里描述的那样:https://www.codementor.io/eh3rrera/using-java-8-method-reference-du10866vx
basic idea ?
List<> list = new ArrayList<>();
list.add(Foo::method1)
list.add(Foo::method2)
list.add(Foo::method3) ...
forEach ..list -> execute
非常感谢
因为一种方法的形式参数和实际参数不同,所以您不能使用方法引用。 Lambdas 应该做的工作:
List<Consumer<Foo>> list = new ArrayList<>();
list.add((foo) -> foo.method1("a"));
list.add((foo) -> foo.method2(new ArrayList<>()));
list.add((foo) -> foo.method3(new HashMap<>(), "Other stuff"));
Foo foo = new Foo();
list.forEach(fooConsumer -> fooConsumer.accept(foo));
我的问题是关于设计的
我正在尝试根据不同的方法设计 order/sequential 调用。
假设我的 Class :
public class Foo {
public Object method1(String a){
// impl1..
}
public Object method2(List<Object> list){
// impl2..
}
public Object method3(Map<String,String> map, Object otherStuff){
// impl3..
}
// other different methods ....
}
我想遵循这个概念 http://qrman.github.io/posts/2017/02/09/pipeline-pattern-plumber-quest
但我的不同之处在于,我使用唯一的 1 class 和多种方法,如果我有不同的服务,我会为每个服务创建新的 class 接口实现,如 [=38] =] ,但我的目的是在 1 class 中创建顺序方法列表,它将迭代并执行它们...
我在想一些基于java 8
的方法参考就像这里描述的那样:https://www.codementor.io/eh3rrera/using-java-8-method-reference-du10866vx
basic idea ?
List<> list = new ArrayList<>();
list.add(Foo::method1)
list.add(Foo::method2)
list.add(Foo::method3) ...
forEach ..list -> execute
非常感谢
因为一种方法的形式参数和实际参数不同,所以您不能使用方法引用。 Lambdas 应该做的工作:
List<Consumer<Foo>> list = new ArrayList<>();
list.add((foo) -> foo.method1("a"));
list.add((foo) -> foo.method2(new ArrayList<>()));
list.add((foo) -> foo.method3(new HashMap<>(), "Other stuff"));
Foo foo = new Foo();
list.forEach(fooConsumer -> fooConsumer.accept(foo));