简体   繁体   English

在 Java 8 中设计/处理不同方法的有序调用的最佳方法是什么

[英]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.

相关问题 在 Java 中存储不同类型对象的有序列表的最佳方法? - Best way to store an ordered list of objects that are of different types in Java? 设计问题 - java - 这样做的最佳方法是什么? - Design question - java - what is the best way to doing this? 在Java EE应用程序中同时进行httpurlconnection调用的最佳方法是什么 - What is the best way to make simultaneous httpurlconnection calls in a Java EE application 通过不同的类同时处理对象列表的最佳方法是什么 - What is the best way to process a object list at the same time by different classes 从java管理unix进程的最佳方法是什么? - What is the best way to manage unix process from java? 用Java处理字符串的最佳方法 - Best way to process strings in Java Java中实现具有相似签名的大量方法的最佳设计模式是什么? - What is the best design pattern in java to implement large set of methods having similar signatures? 将不同的JAVA类合并到一个类中的最佳方法是什么? - what is the best way to combine different JAVA classes in one single class? 在JSP中并行化调用的最佳方法是什么? - What's the best way to parallelize calls in JSP? 什么是通过方法调用的好的 Java 设计? - What is a good Java design to call through methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM