java设计模式之 singleton 单例模式

java设计模式之单例模式singleton


什么是单例模式?

单例模式是:一个类在任何情况下都绝对只有一个实例。


为什么要使用单例模式?

需要控制实例数量,防止实例重复创建、销毁,造成资源浪费。


如何实现单例模式?

  • 需要私有化构造函数
  • 需要提供一个全局访问入口
  • 需要线程安全
  • 需要防止反序列化

单例模式的实现方式,大致有8中写法:

1.饿汉式写法 (推荐)

  • 缺点:
    • 无论是否用到,类加载时就会初始化
    • 不能防止反序列化
  • 优点:
    • 代码实现简单
    • 线程安全 (jvm保证了线程安全)

两种写法

public class Singleton{
	/* 静态成员变量,在类加载时实例化一个对象 */
    private static final Singleton SINGLETON = new Singleton();
	/* 私有构造 */
    private Singleton() {};
	/*全局访问入口 */
    public static Singleton getInstance() {
        return SINGLETON;
    }
}
public class Singleton {
    private static final Singleton SINGLETON;
    /* 静态代码块,类加载时实例化一个对象 */
    static {
        SINGLETON = new Singleton();
    }
	/* 私有构造 */
    private Singleton() {};
	/*全局访问入口 */
    public static Singleton getInstance() {
        return SINGLETON;
    }
}

2.懒汉式写法

  • 缺点:
    • 不能防止反序列化
  • 优点:
    • 按需使用 (只有在用到的时候才会实例化对象)

写法一:线程不安全

public class Singleton{
    private static Singleton SINGLETON;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new Singleton();
        }
        return SINGLETON;
    }
}

写法二:线程安全,但效率降低

public class Singleton{
    private static Singleton SINGLETON;

    private Singleton() {
    }
	/* 加了synchronized 保证了线程安全,但效率降低。*/
    public static synchronized Singleton getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new Singleton();
        }
        return SINGLETON;
    }
}

写法三:双重检查,线程安全,但不够完美

public class Singleton {
    private static volatile Singleton SINGLETON; //JIT

    private Singleton() {
    }
    
    public static Singleton getInstance() {
        if (SINGLETON == null) {
            // 双重检查
            synchronized (Singleton.class) {
                if(SINGLETON == null) {
                    SINGLETON = new Singleton();
                }
            }
        }
        return SINGLETON;
    }
}

写法四:静态内部类写法,线程安全(由 jvm 保证线程安全),相对完美

public class Singleton {

    private Singleton() {
    }
	/* 类加载时不会记载内部类 */
    private static class SingletonHolder {
        private final static Singleton SINGLETON = new Singleton();
    }
	/* 调用getInstance时,加载其内部类,同时实例化内部类的成员变量,实现了懒加载 */
    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

3.枚举写法

比较完美的写法

  • 优点:
    • 防止反序列化
    • 线程安全
public enum Singleton {
    SINGLETON;
  
	/* 测试一下 */
    public static void main(String[] args) {
        for(int i=0; i<100; i++) {
            new Thread(()->{
                System.out.println(Singleton.SINGLETON.hashCode());
            }).start();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XQ0228

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值