One, single case mode get database connection 1, about the definition of singleton mode
Ensure that a class has only one instance and provides a global access point to access it. The singleton implemented in Java is the scope of a virtual machine. Because a virtual machine is loaded with the functionality of a class, a virtual machine creates an instance of a class when it hears its own classload mount a hungry man implementation of the Singleton class. The essence of the Singleton is: the number of control instances . is responsible for creating the unique instance of the Singleton class itself, and provides a getinstance method for external access to the unique instance of the class.
2, the classification of the singleton pattern
Lazy-type will wait until immediately to use the instance to create, is a typical time to change space. A hungry man creates an object instance when the class is loaded, typically for space-time.
3. The steps of the single-instance mode implementation
- Privatization construction Method
Private Singleton () {}
- Defining the properties of a storage instance
// Static lazy-type for use in static methods // static A hungry man is initialized when the class is loaded, and static variables for multiple instances share the same block of memory Private Static Singleton singleton=null;
- Provides a static method for getting an instance
Public Static Singleton getinstance () { return Singleton;}
4. Template implementation
Lazy-Type implementation:
PackageSingletonpattern; Public classLazysingleton {//1. Define a variable to store the class instance Private StaticLazysingleton uniqueinstance=NULL; //2, privatization of the construction method, you can control the creation of an instance of the project in the internal class PrivateLazysingleton () {}//3. Define a method to provide a class instance to the client Public Static synchronizedLazysingleton getinstance () {//determine if the variable for the stored instance has a value//lazy Loading: Do not load resources and data at first, wait until immediately use this resource or data to load//The idea of caching: A typical scheme for space-time change. if(uniqueinstance==NULL){ //if not, create an instance and assign the value to the variable of the storage class instanceUniqueinstance=NewLazysingleton (); } //If there is a value, then use it directly returnuniqueinstance; }}
A hungry man-type implementation
PackageSingletonpattern; Public classHungarysingleton {//1, define a variable to store the created class instance, create an instance here directly, can only create one time Private StaticHungarysingleton uniqueinstance=NewHungarysingleton (); //2, privatization of the construction method, you can control the creation of an instance of the project in the internal class PrivateHungarysingleton () {}//3. Define a method to provide a class instance to the client Public StaticHungarysingleton getinstance () {//use the created instance directly returnuniqueinstance; }}
5. JDBC Connection Instance Challenge
PackageCom.lyjs.jdbcTools;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties;ImportJavax.sql.DataSource;Importorg.apache.commons.dbcp.BasicDataSourceFactory; Public Final classJdbcutils {//single-row mode Privatejdbcutils () {}Private StaticDataSource mydatasource=NULL; //declaring an instance PrivateJdbcutils jdbcutils =Newjdbcutils (); Publicjdbcutils getjdbcutils () {returnjdbcutils; } Static{ Try{class.forname ("Com.mysql.jdbc.Driver"); //load the DBCP configuration fileProperties prop=NewProperties (); InputStream is=jdbcutils.class. getClassLoader (). getResourceAsStream ("Dbcpconfig.properties"); Prop.load (IS); //Create a connection poolMydatasource=Basicdatasourcefactory.createdatasource (prop); } Catch(Exception e) {e.printstacktrace (); Throw NewExceptionininitializererror (); } } //Get Data source PublicDataSource Getdatasource () {returnmyDataSource; } //Get links PublicConnection getconnection () {Try { returnmydatasource.getconnection (); } Catch(Exception e) {e.printstacktrace (); } return NULL; } //Close Connection Public voidFree (ResultSet rs, Statement St, Connection conn) {Try { if(rs! =NULL) {rs.close (); } } Catch(SQLException e) {e.printstacktrace (); } finally { Try { if(St! =NULL) {st.close (); } } Catch(SQLException e) {e.printstacktrace (); } finally { Try { if(Conn! =NULL) Conn.close (); } Catch(Exception e) {e.printstacktrace (); } } } }}
Those design patterns in JDBC