设计模式之——单例设计模式

在这之前转过一篇文章(传送门),有讲述过单例模式,在这里重新归纳到专栏《设计模式总览--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】


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