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

在这之前转过一篇文章(传送门),有讲述过单例模式,在这里重新归纳到专栏《设计模式总览--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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值