单例设计模式

需求----当需要保证对象的唯一性,可以通过单例设计模式来保证对象的唯一性。

具体步骤:

    1.将构造函数私有化。

    2.在类中创建一个本类对象。

    3.提供一个方法可以获取到该对象。

例如,当我们抽取一个了JDBC工具类,我们只希望别人来使用它,不允许别人构造实例。可以通过单例设计模式来保证。

单例模式分为两种:饿汉式,懒汉式(延迟加载)

懒汉式可能会出现线程安全问题

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.sql.Statement;
 6 
 7 public final class JdbcUtilsSingle {
 8     private  String url = "jdbc:mysql://localhost:3306/jdbc";
 9     private  String username = "root";
10     private  String password = "sa";
11 
12     private JdbcUtilsSingle() { //私有化构造函数
13 
14     }
15     //饿汉式
16     private static JdbcUtilsSingle instance=new JdbcUtilsSingle();//在类中创建一个本类对象
17     
18     public static JdbcUtilsSingle getInstance(){  //对外提供一个获取该对象的方法
19         return instance;
20     }
21     
22     
23     //懒汉式-延迟加载
24     /*
25     private static JdbcUtilsSingle instance=null;
26     public static JdbcUtilsSingle getInstance(){
27         if(instance==null)
28             instance=new JdbcUtilsSingle();
29             return instance;
30         
31     }*/
32 
33     static {
34 
35         try {
36             Class.forName("com.mysql.jdbc.Driver");
37         } catch (ClassNotFoundException e) {
38 
39             throw new ExceptionInInitializerError(e);
40         }
41 
42     }
43     public  Connection getConnection() throws SQLException{
44         return DriverManager.getConnection(url,username,password);
45     }
46     public void free(ResultSet rs,Connection conn,Statement st){
47         try {
48             if(rs!=null){
49                 rs.close();
50             }
51         } catch (SQLException e) {
52             e.printStackTrace();
53         }finally{
54             try {
55                 if(st!=null){
56                     st.close();
57                 }
58             } catch (SQLException e2) {
59                 e2.printStackTrace();
60             }finally{
61                 if(conn!=null){
62                     try {
63                         conn.close();
64                     } catch (SQLException e) {
65                         
66                         e.printStackTrace();
67                     }
68                 }
69             }
70         }
71     }
72 
73 }

使用时

 1 import java.sql.Connection;
 2 import java.sql.PreparedStatement;
 3 import java.sql.ResultSet;
 4 
 5 
 6 public class SQLread {
 7     public static void main(String[] args) throws Exception {
 8         read("lisi");
 9     }
10     public static void read(String name) throws Exception{
11         Connection conn=null;
12         PreparedStatement ps=null;     //PreparedStatement对传递过来的字符进行过滤,解决sql注入问题
13         ResultSet rs=null;
14         try{
15             JdbcUtilsSingle instance=JdbcUtilsSingle.getInstance();//得到实例对象
16             conn=instance.getConnection();//通过实例对象调用方法
17         
18         String sql="select id,name,birthday,money from user where name=?";
19         //创建语句
20         ps=conn.prepareStatement(sql);
21         ps.setString(1, name);
22         //执行语句
23         rs=ps.executeQuery();
24         
25         while(rs.next()){
26             System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getDate("birthday")+"\t"+rs.getFloat("money"));
27         }
28         }finally{
29             JdbcUtils.free(rs, conn, ps);
30         }
31         
32     }
33 
34 }
原文地址:https://www.cnblogs.com/dafa4java/p/3242031.html