设计模式之单例模式

前言

本人对于设计模式的学习,仅供参考!

一、单例模式

单例模式,属于创建类型的的一种常见软件设计模式。确保一个类只有一个实例,并提供一个全局唯一访问点。

二、单例模式的实现

1.饿汉式

SingletonStudy01:
饿汉式,类加载到内存后,就实例化一个单例,JVM保证线程安全,简单实用推荐使用。唯一缺点:不管用到与否,类加载时完成实例化。

public class SingletonStudy01 {
    //静态变量,所以类加载后就实例化
    private static final SingletonStudy01 INSTANCE=new SingletonStudy01();
    private SingletonStudy01(){
    }
    public static SingletonStudy01 getInstance(){
        return INSTANCE;
    }

    public static void main(String[] args) {
        SingletonStudy01 singletonStudy01=SingletonStudy01.getInstance();
        SingletonStudy01 singletonStudy02=SingletonStudy01.getInstance();
        //地址引用相同
        System.out.println(singletonStudy01==singletonStudy02);//true
    }
}

SingletonStudy02:
和上面其实是同一种,只是初始化交给了静态代码块。

public class SingletonStudy02 {
    //静态变量,所以类加载后就实例化
    private static final SingletonStudy02 INSTANCE;
    static {
        INSTANCE=new SingletonStudy02();
    }
    private SingletonStudy02(){
    }
    public static SingletonStudy02 getInstance(){
        return INSTANCE;
    }

    public static void main(String[] args) {
        SingletonStudy02 singletonStudy01=SingletonStudy02.getInstance();
        SingletonStudy02 singletonStudy02=SingletonStudy02.getInstance();
        //地址引用相同
        System.out.println(singletonStudy01==singletonStudy02);//true
    }
}

2.懒汉式

SingletonStudy03:
虽然达到了按需初始化的目的,但是带来了线程不安全的问题。可以通过加锁进行进一步的优化。

public class SingletonStudy03 {
    private static SingletonStudy03 INSTANCE;

    private SingletonStudy03(){}
    
    public static SingletonStudy03 getInstance(){
        if (INSTANCE==null){
            try{
                Thread.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            INSTANCE=new SingletonStudy03();
        }
        return INSTANCE;
    }
    //打印100次获取的实例的散列码
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{ 
            System.out.println(SingletonStudy03.getInstance().hashCode());
            }).start();
        }
    }
}

SingletonStudy04:
懒汉式,虽然达到了按需初始化的目的,但是带来了线程不安全的问题,通过synchronized可以解决,但同时也带来效率下降,还可更进一步。

public class SingletonStudy04 {
    private static SingletonStudy04 INSTANCE;
    
    private SingletonStudy04(){}
    
    public static synchronized SingletonStudy04 getInstance(){
        if (INSTANCE==null){
            try{
                Thread.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            INSTANCE=new SingletonStudy04();
        }
        return INSTANCE;
    }
    
    //打印100次获取的实例的散列码
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(SingletonStudy04.getInstance().hashCode());
            }).start();
        }
    }
}

SingletonStudy05:
尝试 通过减小同步代码块的方式提高效率,然后不可行,因为判断已然生效。SingletonStudy05为反面例子。

public class SingletonStudy05 {
    private static SingletonStudy05 INSTANCE;

    private SingletonStudy05(){}

    public static  SingletonStudy05 getInstance(){
        if (INSTANCE==null){
            //妄图通过减小同步代码块的方式提高效率,然后不可行。因为判断已生效
            synchronized (SingletonStudy05.class) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                INSTANCE = new SingletonStudy05();
            }
        }
        return INSTANCE;
    }
    //打印100次获取的实例的散列码
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(SingletonStudy05.getInstance().hashCode());
            }).start();
        }
    }
}

SingletonStudy06:
用双重检查的方式,以消除减小同步代码块产生的问题,即可提高效率。

public class SingletonStudy06 {
    //最好加volatile关键字。JIT优化涉及指令重排问题不加可能会导致没有初始化时就返回INSTANCE
    private static volatile SingletonStudy06 INSTANCE;
    private SingletonStudy06(){
    }

    public static SingletonStudy06 getInstance(){
        if (INSTANCE==null){
            //双重检查
            synchronized (SingletonStudy06.class){
                if (INSTANCE==null){
                    try{
                        Thread.sleep(10);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    INSTANCE=new SingletonStudy06();
                }
            }
        }
        return INSTANCE;
    }
}

SingletonStudy07:
一种比较完美的写法,通过静态内部类方式实现。由JVM保证单例, 加载外部类时不会加载内部类,可以实现懒加载,线程安全也由JVM保证,JVM加载类时只加载一次,所以内部类SingletonStudyHolder也只加载一次, 其中的INSTANCE也只有一个。

public class SingletonStudy07 {
    private SingletonStudy07(){
    }
    //静态内部类 private保证外部不可调用确保是只一个实例
    private static class SingletonStudyHolder{
        private final static SingletonStudy07 INSTANCE=new SingletonStudy07();
    }
    //调用getInstance时才会加载这个静态内部类
    public static SingletonStudy07 getInstance(){
        return SingletonStudyHolder.INSTANCE;
    }
    //打印100次获取的实例的散列码
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                    System.out.println(SingletonStudy07.getInstance().hashCode());
            }).start();
        }
    }
}

3.枚举方式实现

SingletonStudy08:
不仅可以解决线程同步,还可以防止反序列化因为枚举类没有构造方法,即便拿到Class文件也不能构建对象。枚举类型及其定义的枚举变量在JVM中都是唯一的。

public enum  SingletonStudy08 {

    INSTANCE;
    //打印100次获取的实例的散列码
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(SingletonStudy08.INSTANCE.hashCode());
            }).start();
        }
    }

}

总结

综上其实有主要的四种实现方式即:饿汉式、懒汉式的双重检查加锁、静态内部类及枚举方式实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值