-1

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

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...

I was thinking about some method reference based on java 8

like described here : 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

VitalyT
  • 1,671
  • 3
  • 21
  • 49
  • Do you want to call `method2BBB` with the output of `method1AAA` (and so on..)? – Thiyagu Nov 05 '18 at 14:28
  • @user7 , edited my question , I need a way to process order method calls and execute them – VitalyT Nov 05 '18 at 14:35
  • Which parameters are given to methods `mehtod[1-3]` ? And what do you want to do with returned objects ? – Benoit Nov 05 '18 at 14:44
  • @ Benoit , nothing to do with return object , just run methods with ordered call. I put return type as Object case it legacy code that cannot be changed. The arguments are different from method to method. – VitalyT Nov 05 '18 at 14:50

1 Answers1

1

Because formal and actual arguments vary from one method to the other, you cannot use method reference. Lambdas should do the job:

    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));
Benoit
  • 5,118
  • 2
  • 24
  • 43