什么是静态工厂方法?什么是“静态工厂”方法?
3 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
getDbConnection
public class DbConnection{ private static final int MAX_CONNS = 100; private static int totalConnections = 0; private static Set<DbConnection> availableConnections = new HashSet<DbConnection>(); private DbConnection(){ // ... totalConnections++; } public static DbConnection getDbConnection(){ if(totalConnections < MAX_CONNS){ return new DbConnection(); }else if(availableConnections.size() > 0){ DbConnection dbc = availableConnections.iterator().next(); availableConnections.remove(dbc); return dbc; }else { throw new NoDbConnections(); } } public static void returnDbConnection(DbConnection dbc){ availableConnections.add(dbc); //... }}
慕容708150
TA贡献1831条经验 获得超4个赞
价值 getInstance 新情况
添加回答
举报
0/150
提交
取消