public class ConnectionPoolManager { public ConnectionPoolManager { // do some initialize work } public Connection getConnection(){ .... } }
public class GlobalObject { PRivate ConnectionPoolManager connectionPoolManager; public GlobalObject () { connectionPoolManager = new ConnectionPoolManager(); ... } public void getConnectionPoolManager() { return connectionPoolManager; } }
public class QueryFunctions { public static Collection findStudentByName(String name){ ConnectionPoolManager = globalObject.getConnectionPoolManager(); Connection connection = connectionPoolManager.getConnection(); // query database } }
public class ConnectionPoolManager { private static instance = null; private ConnectionPoolManager() { // do some initialize work } public static void getInstance() { if (instance == null){ instance = new ConnectionPoolManager(); } return instance; } public Connection getConnection(){ } }
ConnectionPoolManager 的使用
public class QueryFunctions { public static Collection findStudentByName(String name){ ConnectPoolManager connectionPoolManager = ConnectionPoolManager.getInstance(); Connection conn = connectionPoolManager.getConnection(); // query database } }
public class Runtime { private static Runtime currentRuntime = new Runtime(); public static Runtime getRuntime() { return currentRuntime; } /** Don´t let anyone else instantiate this class */