单例模式

作者: spraysss | 来源:发表于2019-10-09 23:01 被阅读0次

1. 饿汉模式

这种设计存在的问题是无法懒加载

public class Singleton1 {
    private Singleton1(){
        
    }
    public static final Singleton1 INSTANCE=new Singleton1();
    public static Singleton1 getInstance(){
        return INSTANCE;
    }
}

2. 非线程安全懒汉模式

这种设计虽然是懒加载但是在多线程环境下会有线程安全的问题

public class Singleton2 {
    private Singleton2() {

    }

    private static Singleton2 INSTANCE;

    public static Singleton2 getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Singleton2();
        }
        return INSTANCE;
    }

}

3. double check synchronized

最小化锁粒度同步,由于jmv 编译器等优化原因会导致空指针异常

public class Singleton3 {
    private Singleton3() {

    }

    private static Singleton3 INSTANCE;

    public static Singleton3 getInstance() {
        if (null == INSTANCE) {
            synchronized (Singleton3.class) {
                if (null == INSTANCE) {
                    INSTANCE = new Singleton3();
                }
            }
        }
        return INSTANCE;
    }
}

4. volatile + double check

使用 volatile + double check保证了可见行,解决了3中有可能出现等空指针异常

public class Singleton4 {
    private Singleton4() {

    }

    private static volatile  Singleton4 INSTANCE;

    public static Singleton4 getInstance() {
        if (null == INSTANCE) {
            synchronized (Singleton4.class) {
                if (null == INSTANCE) {
                    INSTANCE = new Singleton4();
                }
            }
        }
        return INSTANCE;
    }
}

5. 使用静态内部类

巧妙的使用jvm类加载特性,完成使用时一次加载,并且线程安全

public class Singleton5 {
    private Singleton5() {

    }

    private static class Singleton5Holder {
        private static final Singleton5 INSTANCE = new Singleton5();
    }

    public static Singleton5 getInstance() {
        return Singleton5Holder.INSTANCE;
    }
}

6. 使用枚举

利用enum在使用时只初始化一次的特性

public class Singleton6 {
    private Singleton6() {

    }

    private enum SingletonEnum {
        INSTANCE;

        private Singleton6 instance;

        SingletonEnum() {
            instance = new Singleton6();
        }

        private Singleton6 getInstance() {
            return instance;
        }
    }

    public static Singleton6 getInstance() {
        return SingletonEnum.INSTANCE.getInstance();

    }
}

相关文章

  • 【设计模式】单例模式

    单例模式 常用单例模式: 懒汉单例模式: 静态内部类单例模式: Android Application 中使用单例模式:

  • Android设计模式总结

    单例模式:饿汉单例模式://饿汉单例模式 懒汉单例模式: Double CheckLock(DCL)实现单例 Bu...

  • 2018-04-08php实战设计模式

    一、单例模式 单例模式是最经典的设计模式之一,到底什么是单例?单例模式适用场景是什么?单例模式如何设计?php中单...

  • 设计模式之单例模式详解

    设计模式之单例模式详解 单例模式写法大全,也许有你不知道的写法 导航 引言 什么是单例? 单例模式作用 单例模式的...

  • Telegram开源项目之单例模式

    NotificationCenter的单例模式 NotificationCenter的单例模式分析 这种单例模式是...

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • IOS单例模式的底层原理

    单例介绍 本文源码下载地址 1.什么是单例 说到单例首先要提到单例模式,因为单例模式是单例存在的目的 单例模式是一...

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • 单例模式

    单例模式1 单例模式2

  • java的单例模式

    饿汉单例模式 懒汉单例模式

网友评论

    本文标题:单例模式

    本文链接:https://www.haomeiwen.com/subject/cbfftctx.html