recently i tried to create something like this: Create interface with static method that would return instance of class implementing that interface. Implementing class would have private constructor and interface will create instance of that class via reflection. Sample code here:
static Implementation getImplementation(){
Constructor<Implementation> constructor = Implementation.class.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
}
I ommited exception handling. Is this code valid by any chance for some purpose, or is it just horrible anti-pattern?
Aucun commentaire:
Enregistrer un commentaire