OrderDBManager, UserDBManager, ProductDBManager extend DBManager. I'm trying to create a singletons DBManager Factory to get instances of different DBManager type classes.
But I have a problem using generics to return an instance of exact DBManager type class user wants. Right now it only works for DBManager itself. I would appreciate your help!
public class DBManagerFactory {
private static final Map<String, DBManager> instances = new HashMap<>();
private static final String DB_MANAGER_PACKAGE_NAME = "ua.Test.db.";
public static DBManager getInstance(Class<? extends DBManager> dbManagerClass) {
String dbManagerClassName = dbManagerClass.getSimpleName();
DBManager dbManager = instances.get(dbManagerClassName);
if(dbManager == null){
Class clazz = Class.forName(DB_MANAGER_PACKAGE_NAME + dbManagerClassName);
dbManager = (DBManager)clazz.newInstance();
instances.put(dbManagerClassName, dbManager);
}
return dbManager;
}}
Aucun commentaire:
Enregistrer un commentaire