I have an enumeration that will contain my algorithms. I cannot create instances of these classes because I need an application context that is accessible only after the application starts. I want to load the class at runtime when I select by calling getAlgorithm (Context cnx).
How to easily instantiate a class at runtime given its .class (and my constructor accepts arguments)? All my classes are subclasses of the algorithm.
public enum AlgorithmTypes {
ALL_FROM_9_AND_LAST_FROM_10_ID(AlgorithmFactory.AlgorithmAllFrom9AndLastFrom10Impl.class),
ALL_FROM_9_AND_LAST_FROM_10_CURRENCY_ID(AlgorithmFactory.AlgorithmAllFrom9AndLastFrom10Impl.class),
DIVIDE_BY_9_LESS_THAN_100(AlgorithmFactory.AlgorithmAllFrom9AndLastFrom10Impl.class),
TABLES_BEYOND_5_BY_5(AlgorithmFactory.AlgorithmAllFrom9AndLastFrom10Impl.class);
private Class<? extends Algorithm> algorithm;
AlgorithmTypes(Class<? extends Algorithm> c) {
algorithm = c;
}
public Algorithm getAlgorithm(Context cnx) {
return
}
}
source
share