jeudi 29 septembre 2016

Design pattern to handle different inputs working on the similar algorithm

So, this is pretty basic question but I thought I will see if someone could give a nice solution to it.

I have two similar implementations which work on different types of inputs and call same service but different APIs on the basis of the input and also perform some basic operations on the basis of the response of the previous call. I am looking for a better/logical/OOO way to represent this in java code.

public class C1 {

public O1 M1(I1 input) {

      callServiceXMethodA(input.a(), input.b()....)
      callServiceXMethodB(input.a(), input.b()....)
      callServiceYMethodA(input.a(), input.b()....)
      callServiceYMethodB(input.a(), input.b()....)
      extraStep1();

}

}

public class C2 {

public O2 M2(I2 input) {

     callServiceXMethodA(input.a(), input.b()....)
     callServiceXMethodSTU(input.a(), input.b()....)
     callServiceYMethodPQR(input.a(), input.b()....)
     callServiceYMethodB(input.a(), input.b()....)
     extraStep2();

}

}

I do see the a bit of template pattern (with some hierarchy on the input) or the visitor (though I am not too convinced given that there are only two input types).

I was thinking to keep an abstract class to put major chunk and keep abstract methods with whatever needs different values from the concrete implementation and entry point is the concrete class which routes the request with common parameters to the abstract class.

Can someone suggest a way to cleanly do this in OOO way?

Aucun commentaire:

Enregistrer un commentaire