设计模式学习笔记(一)之单例模式

单例模式

  • 作用:保证一个类只有一个实例,并且提供访问这个实例的全局访问点

  • 应用场景有:

    • 数据库连接池
    • spring中,Bean默认是单例
    • Servlet中,每一个Servlet是单例
    • 配置文件的类,一般是单例
  • 优点:

    • 单例只生成一个实例,减少系统的开销
    • 可以设置系统的全局访问点,优化共享资源的访问
  • 实现方式:

    • 饿汉式----线程安全,调用率效率高,不能延时加载

      public class Singleton {
          private static final Singleton instance = new Singleton();
      
          /**
           * 如果只是加载这个类,不调用getInstance(),会造成资源浪费
           */
          private Singleton() {
          }
      
          /**
           * static变量会在类装载时初始化,
           * 不会涉及多个线程对象访问该对象的问题。
           * 虚拟机保证只会装载一次该类,一定不会发生并发访问的问题。
           * 所以,可以省略synchronized关键字
           *
           * @return
           */
          public static synchronized Singleton getInstance() {
              return instance;
          }
      }
      
      
    • 懒汉式----线程安全,调用效率不高,能够延时加载

      public class Singleton {
          private static Singleton instance;
      
          /**
           * 每次调用getInstance()都要同步,并发效率低
           */
          private Singleton() {
          }
      
          /**
           * 延迟加载,用的时候才会加载
           *
           * @return
           */
          public static synchronized Singleton getInstance() {
              if (null == instance) {
                  instance = new Singleton();
              }
              return instance;
          }
      }
      
      
    • 双重检测锁式

      public class Singleton {
          private static Singleton instance = null;
      
          private Singleton() {
          }
      
          public static Singleton getInstance() {
              if (null == instance) {
                  Singleton singleton;
                  synchronized (Singleton.class) {
                      singleton = instance;
                      if (null == singleton) {
                          synchronized (Singleton.class) {
                              if (null == singleton) {
                                  singleton = new Singleton();
                              }
                          }
                          instance = singleton;
                      }
                  }
              }
              return instance;
          }
      }
      
      
    • 静态内部类式----线程安全,调用效率高,可以延时加载

      public class Singleton {
          /**
           * 只有调用getInstance(),才会加载静态内部类。加载类时是线程安全的。
           * instance是static final类型,保证内存中只有这样一个实例,
           * 并且只能被赋值一次,保证了线程安全性.
           * 具有并发高效调用和延迟加载的优势
           */
          private Singleton() {
          }
      
          private static class SingletonClass {
              private static final Singleton instance = new Singleton();
          }
      
          /**
           * 外部类没有static属性,不会像饿汉式一样立即加载对象
           *
           * @return
           */
          public static Singleton getInstance() {
              return SingletonClass.instance;
          }
      
      
      }
      
      
    • 枚举----线程安全,调用效率高,不能延时加载

      public enum Singleton {
          /**
           * 一个枚举的元素,它代表Singleton的一个实例
           */
          INSTANCE;
      
          public void singleton() {
      
          }
      
      }
      
      
  • 使用场景

    • 单例对象占用资源少,不需要延时加载
      • 枚举 > 饿汉式
    • 单例对象占用资源大,需要延时加载
      • 静态内部类 > 懒汉式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值