samedi 18 avril 2015

How to avoid type-witching in Java?


Vote count:

0




I need to write a method which depends on the type we're passing as an argument. For instance:



void foo(Object o){
if(o instanceof Integer){
System.out.println("Integer");
}

if(o instanceof Date){
System.out.println("Date");
}

//And so forth
}


In my particular case the method has much more complex structure, but it doesn't matter here.


Of course if-then-else clauses are things that an object oriented program try to eliminate.


I think polymorhism can be help of here. But I have not realized how it can be helpful. Any suggestions?



asked 1 min ago







How to avoid type-witching in Java?

Aucun commentaire:

Enregistrer un commentaire