??????JAVA??????????t??????
???????????? ???????[ 2012/12/19 9:42:55 ] ????????
??????????????????????????з?????????????static???Σ?????????????????????t???????????????£????????????????????????????????????????????Stack Overflow?????????????????????????????????????????????????????
???????????????????t??????
?????????????????????????????????????ù?????????????????ù???????????????
Foo x = new Foo()
??????ù?????????????????????ù?????????
Foo x = Foo.create()
??????????????????????private????????????????????????????????????????????????????????????????static????????????????÷???????????????????????????
??????ù????????Щ?????????????????????????(subclasses)??????????????????????????? One is that the factory can choose from many subclasses (or implementers of an interface) and return that. ???????????????÷?????????????????????????????????????????????????????Σ?This way the caller can specify the behavior desired via parameters?? without having to know or understand a potentially complex class hierarchy.????
?????????????????????????Щ???????????????????????connection???????????????????????pools of reusable objects???????????????????????????????????ò?????????????????????????????????????????β?????????????????????????????????????????????????????б???????????????????????С???????????????????????????????????????????????????????????????????????????????????null??
??????????????????????????????????
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 db)
{
//...
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11