• 【设计模式】单例模式的7种实现方法


    一、饿汉式-线程安全

    线程安全,但无法实现实例懒加载策略

    1. /**
    2. * 饿汉式
    3. * @author CC
    4. * @version 1.0
    5. * @since2023/10/12
    6. */
    7. public class Singleton {
    8. private static final Singleton singleton = new Singleton();
    9. private Singleton() {
    10. }
    11. public static Singleton getSingleton() {
    12. return singleton;
    13. }
    14. }

    二、懒汉式-线程不安全

    线程不安全,实现了实例懒加载策略。

    1. /**
    2. * 懒汉式
    3. * @author CC
    4. * @version 1.0
    5. * @since2023/10/12
    6. */
    7. public class Singleton2 {
    8. private static Singleton2 singleton = null;
    9. private Singleton2() {
    10. }
    11. public static Singleton2 getSingleton() {
    12. if (singleton == null) {
    13. singleton = new Singleton2();
    14. }
    15. return singleton;
    16. }
    17. }

    三、全局锁式-线程安全

    线程安全,实现了懒加载策略,但是线程同步效率不高。

    1. /**
    2. * 全局锁
    3. * @author CC
    4. * @version 1.0
    5. * @since2023/10/12
    6. */
    7. public class Singleton3 {
    8. private static Singleton3 singleton;
    9. private Singleton3() {
    10. }
    11. public synchronized static Singleton3 getSingleton() {
    12. if (singleton == null) {
    13. singleton = new Singleton3();
    14. }
    15. return singleton;
    16. }
    17. }

    四、静态代码块式-线程安全

    线程安全,类加载时初始化实例,实现了懒加载策略,线程安全。

    1. /**
    2. * 静态代码块
    3. * @author CC
    4. * @version 1.0
    5. * @since2023/10/12
    6. */
    7. public class Singleton4 {
    8. private final static Singleton4 singleton;
    9. private Singleton4() {
    10. }
    11. static {
    12. singleton = new Singleton4();
    13. }
    14. public static Singleton4 getSingleton() {
    15. return singleton;
    16. }
    17. }

    五、双重校验锁式-线程安全

    线程安全,且实现了懒加载策略,同时保证了线程同步时的效率。

    1. /**
    2. * 双重校验锁
    3. * @author CC
    4. * @version 1.0
    5. * @since2023/10/12
    6. */
    7. public class Singleton5 {
    8. private static Singleton5 singleton;
    9. private Singleton5() {
    10. }
    11. public static Singleton5 getSingleton() {
    12. if (singleton == null) {
    13. synchronized (Singleton5.class) {
    14. if (singleton == null) {
    15. singleton = new Singleton5();
    16. }
    17. }
    18. }
    19. return singleton;
    20. }
    21. }

    六、内部类-线程安全

    线程安全(类加载过程本就线程安全),不存在线程同步问题,单例对象在程序第一次调用getInstance()时主动加载 SingletonHolder和其 静态成员INSTANCE,因而实现了懒加载策略。推荐使用。

    1. /**
    2. * 内部类
    3. * @author CC
    4. * @version 1.0
    5. * @since2023/10/12
    6. */
    7. public class Singleton6 {
    8. private Singleton6() {
    9. }
    10. private static class Singleton6Holder {
    11. private static final Singleton6 INSTANCE = new Singleton6();
    12. }
    13. public static Singleton6 getSingleton() {
    14. return Singleton6Holder.INSTANCE ;
    15. }
    16. }

    七、枚举类-线程安全

    线程安全,不存在线程同步问题,且单例对象在枚举类型 INSTANCE第一次引用时通过枚举的 构造函数初始化,从而实现了懒加载策略。推荐使用。

    1. /**
    2. * 枚举类
    3. * @author CC
    4. * @version 1.0
    5. * @since2023/10/12
    6. */
    7. public class Singleton7 {
    8. private Singleton7() {
    9. }
    10. enum SingletonEnum {
    11. INSTANCE;
    12. private final Singleton7 singleton;
    13. private SingletonEnum() {
    14. singleton = new Singleton7();
    15. }
    16. }
    17. public static Singleton7 getSingleton() {
    18. return SingletonEnum.INSTANCE.singleton;
    19. }
    20. }

  • 相关阅读:
    WinRAR功能之【自动加密】
    2.10-CSS基础--盒子模型(下)
    成都理工大学_Python程序设计_第9章
    数据结构与算法之(赫夫曼树,哈夫曼树,压缩软件)
    Vue路由懒加载
    C# 自定义事件
    MacOS Ventura 13.0 Beta5 (22A5321d) 带 OC 0.8.4 三分原版黑苹果镜像
    SSM学习45:设置请求映射路径,避免路径相同
    常见视频传输接口
    C++:拷贝构造函数的初始化列表
  • 原文地址:https://blog.csdn.net/c1390527393/article/details/133800870