I have a use case with a class existing in 2 versions of a package.
package packageV1;
public class MyClass extends BaseClass{
public static String example(){
return "Version1";
}
}
package packageV2;
public class MyClass extends BaseClass{
public static String example(){
return "Version2";
}
}
So far so good (I believe).
Then I have an application using that class, and to avoid rewriting the application for the different package version, I want to pass the class that should be use (ie for the package of interest) as argument to the application. So something like
public class Application{
private Class<BaseClass> selectedClass;
public void Application(Class<BaseClass> selectedClass){
this.selectedClass = selectedClass;
this.selectedClass.example(); // not possible
}
}
I believe I could call this.selectedClass.example();
if I were passing an instance of MyClass
in the constructor, but then I would call static methods through a instance object, not nice right ?
On the other hand, in the example above selectedClass
is a Class object, so I can't call the static method example
as above.
Does this mean I should use reflection ? like selectedClass.getMethod(name, parameterTypes)
. Looks overly complicated to me.
Or is there a better design ?
Aucun commentaire:
Enregistrer un commentaire