美文网首页设计模式
设计模式-单例

设计模式-单例

作者: isLJli | 来源:发表于2020-04-14 17:30 被阅读0次

单例模式

饿汉模式
不足:由于static在类加载时就会开始创建,无法对实例做延时加载。

public class HuangurySingleton {
  private static final  HuangurySingleton HUANGURY_SINGLETON= new HuangurySingleton();

  private HuangurySingleton(){}

  public static HuangurySingleton getHuangurySingleton(){
      return HUANGURY_SINGLETON;
  }
}

优化:懒汉
懒汉模式

public class LazySingleton {
  
  private static LazySingleton instance;
  
  private LazySingleton(){}
  
  private static LazySingleton getInstance(){
      if(instance == null){
          instance= new LazySingleton();
      }
      return instance;
  }
}

缺点:在多线程并发下实例并不是唯一
优化:懒汉线程安全
懒汉线程安全

public class LazySafeSingleton {

  private static LazySafeSingleton instance;

  private LazySafeSingleton(){}

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

  public static synchronized LazySafeSingleton getInstance1(){
      if(instance == null){
          instance = new LazySafeSingleton();
      }
      return instance;
  }
}

缺点:性能效率
优化:DCL
DCL模式

public class DclSingleton {
  private static volatile DclSingleton instance;

  private DclSingleton(){}

  private static DclSingleton getInstance(){
      if(instance==null){
          synchronized (DclSingleton.class){
              if(instance == null){
                  instance = new DclSingleton();
              }
          }
      }
      return instance;
  }
}

缺点:如果没有加volatile,其他子线程可能出现数据不同步问题。
优化:静态内部类/枚举
静态内部类

public class StaticInnerSingleton {
  private StaticInnerSingleton(){}

  public static StaticInnerSingleton getInstance(){
      return SingletonHolder.STATIC_INNER_SINGLETON;
  }

  private static class SingletonHolder{
      private static final StaticInnerSingleton STATIC_INNER_SINGLETON= new StaticInnerSingleton();
  }
}

优点:JVM本身保证了线程安全/没有性能消耗
原因:static,final

总结:

  • 饿汉:无法满足延迟加载
  • 懒汉:无法保证线程安全
  • 懒汉线程安全:无法保证性能
  • DCL: 数据的同步
  • 静态内部类/枚举:延迟加载/线程安全/性能优势

android中单例模式:

单例模式的造成的内存泄漏
如果有Context的构造参数传入,由于单例的static的生命的周期是整个应用的,如果activity的context传进来,就会造成actviity无法被回收。

相关文章

  • 单例模式Java篇

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

  • python中OOP的单例

    目录 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • 单例

    目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • python 单例

    仅用学习参考 目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计...

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

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

  • 设计模式第二篇、单例设计模式

    目录1、什么是单例设计模式2、单例设计模式的简单实现3、单例设计模式面临的两个问题及其完整实现4、单例设计模式的应...

  • 设计模式 - 单例模式

    设计模式 - 单例模式 什么是单例模式 单例模式属于创建型模式,是设计模式中比较简单的模式。在单例模式中,单一的类...

  • 2、创建型设计模式-单例设计模式

    江湖传言里的设计模式-单例设计模式 简介:什么是单例设计模式和应用 备注:面试重点考查 单例设计模式:这个是最简单...

  • 设计模式之单例模式

    单例设计模式全解析 在学习设计模式时,单例设计模式应该是学习的第一个设计模式,单例设计模式也是“公认”最简单的设计...

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

网友评论

    本文标题:设计模式-单例

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