I have a java code like this-
Sample obj;
if(obj instanceOf Class1)
{
method1(obj);
} else if(obj instanceOf Class2)
{
method2(obj);
} else if(obj instanceOf Class3)
{
method3(obj);
}
Class1, Class2 and Class3 are actually DTO's and they are children of Sample, so I don't want to write business logic in DTO's.
I want to write the logic of method1, mehtod2 and method3 in a different class. Is there a design pattern for that. I am using spring MVC.
Basically, I am looking for some non-java technique to call the appropriate based on the instance instead of checking with instanceOf every single time.