設計模式之——單例設計模式

在這之前轉過一篇文章(傳送門),有講述過單例模式,在這裏重新歸納到專欄《設計模式總覽--Java版》(飛機票)。

單例模式,是我們日常開發中最容易使用到的模式之一,他主要有 以下幾種寫法:

package com.zndroid.dm.SingletonModel;

/**
 * Created by luzhenyu on 2016/8/13.
 */
public class SingletonModel {

    ///===================================================================================
    ///單例模式:
    ///惡漢式(基本已經滿足日常開發需要)
    ///線程不安全/性能較懶漢式差點
    ///===================================================================================
    /*private static final SingletonModel instance = new SingletonModel();
    private SingletonModel () {}
    public static SingletonModel getInstance () {
        return  instance;
    }*/

    ///===================================================================================
    ///單例模式:
    ///懶漢式
    ///線程不安全
    ///===================================================================================
    /*private static SingletonModel instance;
    private SingletonModel () {}
    public static SingletonModel getInstance () {
        if (instance == null)
           instance = new SingletonModel();

        return instance;
    }*/

    ///===================================================================================
    ///單例模式:
    ///優化版加鎖
    ///線程安全
    ///在上一種方式之後,我們考慮在獲取實例時時加鎖,不爲空直接返回,性能一般
    ///===================================================================================
    /*private static SingletonModel instance = null;
    private SingletonModel () {}
    public static SingletonModel getInstance () {
        synchronized (SingletonModel.class) {
            if (instance == null)
                instance = new SingletonModel();
        }

        return instance;
    }*/

    ///===================================================================================
    ///單例模式:
    ///雙重鎖
    ///線程安全
    ///在上一種方式之後,我們考慮在instance爲null時加鎖,不爲空直接返回,性能較好
    ///===================================================================================
    /*private static SingletonModel instance = null;
    private SingletonModel () {}
    public static SingletonModel getInstance () {
        if (instance == null) {
            synchronized (SingletonModel.class) {
                if (instance == null)
                    instance = new SingletonModel();
            }
        }

        return instance;
    }*/

    ///===================================================================================
    ///單例模式:
    ///雙重鎖優化版
    ///線程安全
    ///===================================================================================
    /*private static volatile SingletonModel instance = null;
    private SingletonModel () {}
    public static SingletonModel getInstance () {
        SingletonModel temp = instance;

        if (temp == null) {
            synchronized (SingletonModel.class) {
                temp = instance;
                if (temp == null) {
                    temp = new SingletonModel();
                    instance = temp;
                }
            }
        }

        return instance;
    }*/

    ///===================================================================================
    ///單例模式:
    ///個人比較推薦的一種寫法,簡單/不受版本限制/思路來自於JVM特性
    ///線程安全
    ///===================================================================================
    private SingletonModel () {}

    private static class SingletonTemp {
        private static final SingletonModel instance = new SingletonModel();
    }

    public static SingletonModel getInstance () {
        return SingletonTemp.instance;
    }

    ///===================================================================================
    ///單例模式:
    ///枚舉 最簡單的處理方式,但是官方表示“Enums often require more than twice as much memory as static constants.”即會佔用大量內存,所以慎用枚舉。
    ///線程安全,自動支持序列化機制,不能通過 reflection attack 來調用私有構造方法
    ///===================================================================================
    public enum Singleton {
        INSTANCE;
    }
}

【歡迎上碼】

【微信公衆號搜索 h2o2s2】


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章