1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Factory {
public Solver getSolver(String className) throws Exception {
// On récupère l'objet représentant la classe :
Class pClass = Class.forName(className);
// throw ClassNotFoundException
// On instancie une nouvelle instance :
Object instance = pClass.newInstance();
// throw InstantiationException, IllegalAccessException
// Si l'instance crée n'est pas une instance de Solver
if ( !(instance instanceof Solver) ) {
throw new Exception ("La classe n'implemente pas Solver");
}
return (Solver) instance;
}
} |