I'm trying to reduce some code duplication. Currently i got two methods that are almost identical, the major difference being calling two separate methods within them.
Below is basically what i wanna do:
private void combinedMethod(SomeClass sc, Class c)
{
Method m = null;
if(c instanceof SubClass1)
m = sc.Method();
if(c instanceof SubClass2)
m = sc.AnotherMethod();
m(c.getA(), c.getB());
}
I've tested (and it works) this using reflection. But is there a better way of doing it? I read somewhere that reflection is slow and only to be used as a last resort. Is it in this case?
Also in this case the SomeClass is a standard class in the java api. The Class I send in is of my own making.
Aucun commentaire:
Enregistrer un commentaire