java - What are static factory methods? -


what's "static factory" method?

we avoid providing direct access database connections because they're resource intensive. use static factory method getdbconnection creates connection if we're below limit. otherwise, tries provide "spare" connection, failing exception if there none.

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);      //...    } } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -