![](/img/trans.png)
[英]Best way to store an ordered list of objects that are of different types in Java?
[英]What is the best way to design/process in Java 8 ordered calls on different methods
我的問題是關於設計
我正在嘗試根據不同的方法設計順序/順序調用。
假設我的班級:
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 個具有多種方法的類,如果我有不同的服務,我將使用鏈接中的接口實現為每個服務創建新類,但我的目的是在 1 個類中創建訂單方法列表,該類將迭代並執行它們......
我在考慮一些基於 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
非常感謝
因為形式參數和實際參數因一種方法而異,所以不能使用方法引用。 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.