为了学习工厂模式,写了一段代码,可是问题出现了Connection
public interface Connection {
void executeQuery(String sql);
void close();
}Driverpublic class Driver {
public static Connection getConnection(String connection){
if(connection.equalsIgnoreCase("COM.wqs.WangConnection")){
return new WangConnection();
}else if(connection.equals("com.wqs.QiuConnection")){
return new QiuConnection();
}else{
return null;
}

}
}
我的Driver,感觉,很弱,一旦有新的驱动,我就得往里加东西,于是我想, 如果参数传进啥,就能new出来啥,是不是,就特别完美了呢,于是,我就看了传说中的抽象工厂模式,可惜没看懂,也看不进去,于是想偷个懒 嘿嘿,前辈指点,先谢谢了

解决方案 »

  1.   

    return Class.forName(connection).newInstance();
      

  2.   

    在真正连接db时,往往Driver 和 Class.forname,是分开的,Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();Connection con= DriverManager.getConnection(url,uid,pwd); 那在DriverManager是如何得到Class.forname返回的实例呢? 
      

  3.   

    请问,这句是什么意思啊
    // Returns the caller's class loader, or null if none
        private static native ClassLoader getCallerClassLoader();