I am currently trying to reorganize a part of the project that looks like this:
Many classes
B extends A; C extends A; D extends C; E extends B; F extends A; ...
And somewhere in the code:
if (x instanceof B){ B n = (B) x; ... }else if (x instanceof C){ C n = (C) x; ... }else if (x instanceof D){ D n = (D) x; ... }else if (x instanceof E){ E n = (E) x; ... }else if (x instanceof G){ G n = (G) x; ... }...
Above if-construct, it currently sits in a function with CC 19. Now my question is: can I separate this if-construct in mutliple functions and let Java OO do the magic? Or are there any catches I should look for?
My idea:
private void oopMagic(C obj){ ... Do the stuff from the if(x instanceof C) here} private void oopMagic(D obj){ ... Do the stuff from the if(x instanceof D) here} private void oopMagic(E obj){ ... Do the stuff from the if(x instanceof E) here} ....
and instead of a huge if:
oopMagic(x);
Edit: I cannot change any of the classes (A, B, C, ...). Inside if statements, some getters are used to read (never write) data from each object.
source share