The advantages of the factory method here is not to say, online search a lot of yourself to see
If you use the JDBC link database and the database switches from MySQL to Oracle, the change is to switch the name of the driver, and the others do not need to be modified, which is a direct example of factory method flexibility.
Empty talk
On the code:
Public Abstract classAbstractsqlfactory {//This is an example of a generic class that needs to be controlled in the JDK1.5 feature Public Abstract<textendsSqlutil> T Createsql (class<t>c); }==================================== PackageCOM.FXR. Application of factory method mode;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.Statement; Public InterfaceSqlutil {//connection to the database PublicConnection getconnection (); //implementation of methods for shutting down the database Public voidClosedb (Connection conn, Statement stmt, ResultSet rs); }==================================== PackageCOM.FXR. Application of factory method mode; Public classSqlfactoryextendsabstractsqlfactory{@Override Public<textendsSqlutil> T Createsql (class<t>c) {sqlutil Sqlutil=NULL; Try{sqlutil=(Sqlutil) Class.forName (C.getname ()). Newinstance (); }Catch(Exception e) {System.out.println ("There was an error! "); } return(T) sqlutil; }}========================================== PackageCOM.FXR. Application of factory method mode;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classMysqlutilimplImplementssqlutil{Private StaticString Driver = "Com.mysql.jdbc.Driver";//database-driven strings Private StaticString url = "Jdbc:mysql://localhost/studentsystem?useunicode=true&characterencoding=utf-8";//Connection URL Private StaticString user = "Yonghumingcheng";//Database user name Private StaticString password = "Mima";//User Password@Override PublicConnection getconnection () {Connection con=NULL; Try{class.forname (driver); }Catch(ClassNotFoundException e) {System.out.println ("Load driver error message:" +e.getmessage ()); } Try{con=drivermanager.getconnection (Url,user,password); }Catch(SQLException e) {System.out.println ("Database connection Error message:" +e.getmessage ()); E.printstacktrace (); } returncon; } @Override Public voidClosedb (Connection conn, Statement stmt, ResultSet rs) {//If the result set object is not empty, close the if(rs! =NULL) { Try{rs.close (); } Catch(Exception e) {e.printstacktrace (); } } //If the statement object is not empty, close the if(stmt! =NULL) { Try{stmt.close (); } Catch(Exception e) {e.printstacktrace (); } } //If the database connection object is not empty, close the if(Conn! =NULL) { Try{conn.close (); } Catch(Exception e) {e.printstacktrace (); } } } }=========================================== PackageCOM.FXR. Application of factory method mode;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classSqlserverutilimplImplementssqlutil{PrivateString Driver = "Com.microsoft.jdbc.sqlserver.SQLServerDriver"; Private Static FinalString uri= "jdbc:sqlserver://127.0.0.1:1433; Databasename=demo.db "; PrivateString userName = "Yonghumingcheng"; PrivateString password = "Mima"; @Override PublicConnection getconnection () {Connection con=NULL; Try{class.forname (driver); }Catch(ClassNotFoundException e) {System.out.println ("Load driver error message:" +e.getmessage ()); } Try{con=drivermanager.getconnection (Driver,username,password); }Catch(SQLException e) {System.out.println ("Database connection Error message:" +e.getmessage ()); E.printstacktrace (); } returncon; } @Override Public voidClosedb (Connection conn, Statement stmt, ResultSet rs) {//If the result set object is not empty, close the if(rs! =NULL) { Try{rs.close (); } Catch(Exception e) {e.printstacktrace (); } } //If the statement object is not empty, close the if(stmt! =NULL) { Try{stmt.close (); } Catch(Exception e) {e.printstacktrace (); } } //If the database connection object is not empty, close the if(Conn! =NULL) { Try{conn.close (); } Catch(Exception e) {e.printstacktrace (); } } }}
The realization of the factory method in the actual combat of the big talk design pattern--Link database application