??????????????????????????з?????????????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)
   {
     //...
   }
}