[英]What is the best way to design/process in Java 8 ordered calls on different methods
My question is regarding design我的问题是关于设计
I'm trying to design order/sequential calls based on different methods.我正在尝试根据不同的方法设计顺序/顺序调用。
Suppose my 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 ....
}
I want to follow this concept http://qrman.github.io/posts/2017/02/09/pipeline-pattern-plumber-quest我想遵循这个概念http://qrman.github.io/posts/2017/02/09/pipeline-pattern-plumber-quest
But my difference is that I use the only 1 class with multiple methods, if i had different services I would create new class to each service with interface implementation as in the link , but my purpose is create list of order methods inside 1 class that will iterate and execute them...但我的不同之处在于,我使用只有 1 个具有多种方法的类,如果我有不同的服务,我将使用链接中的接口实现为每个服务创建新类,但我的目的是在 1 个类中创建订单方法列表,该类将迭代并执行它们......
I was thinking about some method reference based on java 8我在考虑一些基于 java 8 的方法参考
like described here : https://www.codementor.io/eh3rrera/using-java-8-method-reference-du10866vx就像这里描述的: 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
Thanks a lot非常感谢
Because formal and actual arguments vary from one method to the other, you cannot use method reference.因为形式参数和实际参数因一种方法而异,所以不能使用方法引用。 Lambdas should do the job:
Lambda应该完成这项工作:
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.