vendredi 8 juillet 2022

Best way to refactor methods that have the same code but different return values

I have this piece of code:

public A methodA(X answer) {
  ... 
  return A;
}

public B methodB(X answer) {
  ... 
  return B;
}

So, for example, I have two call those methods in different methods, because the return type is different, so for example, on the controller:

public A version1(X answer) {
 ...
 A ant = methodA();
 ...
}

public Bversion2(X answer) {
 ...
 B bee = methodB();
 ...
}

The point is that every other single line of code is the same, except the calls specified. How can I refactor this code in Java to avoid using duplicated code?

Thanks!

Aucun commentaire:

Enregistrer un commentaire