每日一發(fā)設(shè)計(jì)模式 - 單例模式(singleton)

singleton

單例模式,是一種常用的軟件設(shè)計(jì)模式。在它的核心結(jié)構(gòu)中只包含一個(gè)被稱為單例的特殊類。通過單例模式可以保證系統(tǒng)中,應(yīng)用該模式的類一個(gè)類只有一個(gè)實(shí)例。即一個(gè)類只有一個(gè)對象實(shí)例。

為什么要用單例模式

在實(shí)際的項(xiàng)目中,有一些東西是大家共用的,比如:系統(tǒng)配置、redis實(shí)例、數(shù)據(jù)庫實(shí)例等,我們不希望每個(gè)用戶在使用這些資源的時(shí)候,自己去新建一份實(shí)例,這樣會(huì)造成資源的浪費(fèi)。
目的:保證一個(gè)類只有一個(gè)實(shí)例
使用場景:當(dāng)你想控制實(shí)例個(gè)數(shù),節(jié)省系統(tǒng)資源的時(shí)候
解決方案:判斷該類是否已經(jīng)有實(shí)例,如果有則返回,沒有則創(chuàng)建
關(guān)鍵代碼:私有化構(gòu)造函數(shù),讓使用方不能隨意的去實(shí)例
實(shí)現(xiàn)方案:懶漢、餓漢、靜態(tài)內(nèi)部類、枚舉

  1. 懶漢 - 線程不安全
package com.my.test.singleton;

//懶漢 - 線程不安全
public class SingletonExample01 {

    private SingletonExample01(){

    }

    private static SingletonExample01 instance = null;

    public static SingletonExample01 getInstance(){
        if(instance == null){
            instance = new SingletonExample01();
        }
        return instance;
    }
}
  1. 懶漢 - 線程安全
package com.my.test.singleton;

//懶漢 - 線程安全
public class SingletonExample02 {

    private SingletonExample02(){

    }

    private static SingletonExample02 instance = null;

    public static synchronized SingletonExample02 getInstance(){
        if(instance == null){
            instance = new SingletonExample02();
        }
        return instance;
    }
}
  1. 雙重校驗(yàn)鎖
package com.my.test.singleton;

//懶漢 - 雙重校驗(yàn)鎖
public class SingletonExample03 {

    private SingletonExample03(){

    }

    private static volatile SingletonExample03 instance = null;

    public static SingletonExample03 getInstance(){
        if(instance == null){
            synchronized (SingletonExample03.class){
                if(instance == null){
                    instance = new SingletonExample03();
                }
            }
        }
        return instance;
    }
}
  1. 餓漢模式
package com.my.test.singleton;

//餓漢模式
public class SingletonExample04 {

    private SingletonExample04(){

    }

    // 方法一
//    private static SingletonExample04 instance = new SingletonExample04();


    // 方法二
    private static SingletonExample04 instance = null;

    static {
        instance = new SingletonExample04();
    }

    public static SingletonExample04 getInstance(){
        return instance;
    }
}
  1. 靜態(tài)內(nèi)部類
package com.my.test.singleton;

// 靜態(tài)內(nèi)部類
public class SingletonExample05 {

    private SingletonExample05(){

    }

    private static class InstanceSingletonExample05{
        private static SingletonExample05 instance = new SingletonExample05();
    }

    public static SingletonExample05 getInstance(){
        return InstanceSingletonExample05.instance;
    }
}
  1. 枚舉
package com.my.test.singleton;


// 枚舉
public class SingletonExample06 {

    private SingletonExample06(){
    }

    public static SingletonExample06 getInstance(){
        return InstanceSingletonExample06.INSTANCE.getSingletonExample06();
    }

    private enum InstanceSingletonExample06{
        INSTANCE;

        private SingletonExample06 singletonExample06 = new SingletonExample06();

        public SingletonExample06 getSingletonExample06(){
            return singletonExample06;
        }
    }
}
  1. 枚舉2.0
package com.my.test.singleton;

public enum SingletonEnumExample{
    INSTANCE;

    public void test(){
        // do everything
    }
}

實(shí)例場景之 - 獲取jdbc連接

我們新增兩個(gè)類,第一個(gè)是非單例的獲取jdbc連接

package com.mk.designDemo.singleton;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MDataSourceNoSingle {

    private static final Logger logger = LoggerFactory.getLogger(MDataSourceNoSingle.class);

    private static Connection connection = null;

    public Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://******:3306/*****?characterEncoding=UTF-8", "root", "root");
            logger.info("線程{}實(shí)例化connection", Thread.currentThread().getName());
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

}

第二個(gè)使用了單例模式

package com.mk.designDemo.singleton;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MDataSourceSingle {

    private static final Logger logger = LoggerFactory.getLogger(MDataSourceSingle.class);

    private static Connection connection = null;

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://******:3306/*****?characterEncoding=UTF-8", "root", "root");
            logger.info("線程{}實(shí)例化connection", Thread.currentThread().getName());
        } catch (ClassNotFoundException | SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }
    
    private MDataSourceSingle() {
    }

    public static Connection getConnection() {
        return connection;
    }
}

然后我們寫了一個(gè)簡單的controller,里面有兩個(gè)接口,用來驗(yàn)證結(jié)果

package com.mk.designDemo.controller;

import com.mk.designDemo.singleton.MDataSourceNoSingle;
import com.mk.designDemo.singleton.MDataSourceSingle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;

@RestController
public class SingletonController {

    private static final Logger logger = LoggerFactory.getLogger(SingletonController.class);

    private final static String sql = "select * from t_trade_date limit 10";

    @RequestMapping(path = "/noSingleton")
    public String noSingleton() {
        MDataSourceNoSingle mDataSourceNoSingle = new MDataSourceNoSingle();
        Connection connection = mDataSourceNoSingle.getConnection();
        doExecute(connection, sql);
        return "OK";
    }

    @RequestMapping(path = "/singleton")
    public String singleton() {
        doExecute(MDataSourceSingle.getConnection(), sql);
        return "OK";
    }


    private void doExecute(Connection connection, String sql) {
        logger.info("do execute sql:{}", sql);
    }

}

當(dāng)我們調(diào)用三次非單例的接口時(shí),查看日志輸出如下,connection被實(shí)例化了多次:

2019-07-09 15:47:31.966 [http-nio-80-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - GET "/web/noSingleton", parameters={}
2019-07-09 15:47:31.970 [http-nio-80-exec-1] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to public java.lang.String com.mk.designDemo.controller.SingletonController.noSingleton()
2019-07-09 15:47:32.633 [http-nio-80-exec-1] INFO  c.m.d.singleton.MDataSourceNoSingle - 線程http-nio-80-exec-1實(shí)例化connection
2019-07-09 15:47:32.633 [http-nio-80-exec-1] INFO  c.m.d.controller.SingletonController - do execute sql:select * from t_trade_date limit 10
2019-07-09 15:47:32.671 [http-nio-80-exec-1] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-07-09 15:47:32.672 [http-nio-80-exec-1] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Writing ["OK"]
2019-07-09 15:47:32.693 [http-nio-80-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Completed 200 OK
2019-07-09 15:47:40.005 [http-nio-80-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - GET "/web/noSingleton", parameters={}
2019-07-09 15:47:40.007 [http-nio-80-exec-4] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to public java.lang.String com.mk.designDemo.controller.SingletonController.noSingleton()
2019-07-09 15:47:40.361 [http-nio-80-exec-4] INFO  c.m.d.singleton.MDataSourceNoSingle - 線程http-nio-80-exec-4實(shí)例化connection
2019-07-09 15:47:40.361 [http-nio-80-exec-4] INFO  c.m.d.controller.SingletonController - do execute sql:select * from t_trade_date limit 10
2019-07-09 15:47:40.362 [http-nio-80-exec-4] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-07-09 15:47:40.362 [http-nio-80-exec-4] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Writing ["OK"]
2019-07-09 15:47:40.363 [http-nio-80-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Completed 200 OK
2019-07-09 15:47:48.459 [http-nio-80-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - GET "/web/noSingleton", parameters={}
2019-07-09 15:47:48.461 [http-nio-80-exec-7] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to public java.lang.String com.mk.designDemo.controller.SingletonController.noSingleton()
2019-07-09 15:47:48.801 [http-nio-80-exec-7] INFO  c.m.d.singleton.MDataSourceNoSingle - 線程http-nio-80-exec-7實(shí)例化connection
2019-07-09 15:47:48.801 [http-nio-80-exec-7] INFO  c.m.d.controller.SingletonController - do execute sql:select * from t_trade_date limit 10
2019-07-09 15:47:48.802 [http-nio-80-exec-7] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-07-09 15:47:48.802 [http-nio-80-exec-7] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Writing ["OK"]
2019-07-09 15:47:48.803 [http-nio-80-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - Completed 200 OK

然后我們調(diào)用單例的接口三次,日志輸出如下,我們可以看到,只有第一次進(jìn)行了實(shí)例化,后面再也沒有繼續(xù)實(shí)例化

2019-07-09 15:49:22.868 [http-nio-80-exec-10] DEBUG o.s.web.servlet.DispatcherServlet - GET "/web/singleton", parameters={}
2019-07-09 15:49:22.871 [http-nio-80-exec-10] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to public java.lang.String com.mk.designDemo.controller.SingletonController.singleton()
2019-07-09 15:49:23.234 [http-nio-80-exec-10] INFO  c.m.d.singleton.MDataSourceSingle - 線程http-nio-80-exec-10實(shí)例化connection
2019-07-09 15:49:23.235 [http-nio-80-exec-10] INFO  c.m.d.controller.SingletonController - do execute sql:select * from t_trade_date limit 10
2019-07-09 15:49:23.237 [http-nio-80-exec-10] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-07-09 15:49:23.237 [http-nio-80-exec-10] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Writing ["OK"]
2019-07-09 15:49:23.239 [http-nio-80-exec-10] DEBUG o.s.web.servlet.DispatcherServlet - Completed 200 OK
2019-07-09 15:49:25.032 [http-nio-80-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - GET "/web/singleton", parameters={}
2019-07-09 15:49:25.034 [http-nio-80-exec-1] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to public java.lang.String com.mk.designDemo.controller.SingletonController.singleton()
2019-07-09 15:49:25.034 [http-nio-80-exec-1] INFO  c.m.d.controller.SingletonController - do execute sql:select * from t_trade_date limit 10
2019-07-09 15:49:25.035 [http-nio-80-exec-1] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-07-09 15:49:25.036 [http-nio-80-exec-1] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Writing ["OK"]
2019-07-09 15:49:25.038 [http-nio-80-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Completed 200 OK
2019-07-09 15:49:25.926 [http-nio-80-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - GET "/web/singleton", parameters={}
2019-07-09 15:49:25.926 [http-nio-80-exec-2] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to public java.lang.String com.mk.designDemo.controller.SingletonController.singleton()
2019-07-09 15:49:25.927 [http-nio-80-exec-2] INFO  c.m.d.controller.SingletonController - do execute sql:select * from t_trade_date limit 10
2019-07-09 15:49:25.928 [http-nio-80-exec-2] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-07-09 15:49:25.928 [http-nio-80-exec-2] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Writing ["OK"]
2019-07-09 15:49:25.929 [http-nio-80-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Completed 200 OK

驗(yàn)證通過,OVER!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容