I have interface AAA. I have two classes (entities):
public class BBB implements AAA
public class CCC implements AAA
Both classes have different fields of course that will be used in Service
I have Service:
public class Service() {
public DDD returnDdd(AAA aaa){
....
return method1(aaa);
}
private DDD method1(AAA aaa){
if(aaa instanceof BBB){
return method2(aaa)
}
return method3(aaa)
}
}
How to avoid instanceof? In my Service I have common logic for both BBB and CCC. Method1 and Method2 use the same private methods in this service.
In this case I would recommend to have a common method in the interface AAA, like:
and make both inherited classes to implement this method. In this case you will have to call only one method which will be automatically chosen based on the exact instance.