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

Android-设计模式-单例模式

作者: 是刘航啊 | 来源:发表于2019-07-31 14:54 被阅读0次
什么是单例 ?
  • 保证一个类只有一个实例
  • 保证没有其他实例可以被创建,并且只提供一个访问该实例的方法
为什么使用单例 ?
  • 节约资源,避免多次创建

单例模式 🌰

饿汉模式 - 线程安全
public class SingleHungryClass {
  
  static final SingleHungryClass single_hungry = new SingleClass();  

  private SingleHungryClass() {

  }

  public static SingleHungryClass getSingleHungry() {
      return single_hungry;
  }

}

优点:线程安全
缺点:没有延迟加载节约资源

懒汉模式(DCL) - 线程安全
public class SingleLazyClass {

  static volatile SingleLazyClass single_lazy = null;

  private SingleLazyClass() {

  }

  public static SingleLazyClass getSingleLazy() {
        if (single_lazy == null) {
            synchronized (SingleClass.class) {
                if (single_lazy == null) {
                    single_lazy = new SingleClass();
                }
            }
        }
      return single_lazy;
  }

}

优点:线程安全,延迟加载
缺点:首次加载会比较慢

内部类 - 线程安全
public class SingleClass {

  private SingleClass() {

  }
  
  static class SingleInstance {
        static final SingleClass singleClass = new SingleClass();
  }
  
  public static SingleClass getSingle() {
        return SingleInstance.singleClass;
  }

}

优点:线程安全,延迟加载

枚举
public enum SingleEnumClass {

  INSTANCE;
  
  SingleEnumClass() {

  }

}

优点:线程安全
缺点:消耗资源(官方不推荐使用枚举 )
官方链接

A single enum can add about 1.0 to 1.4 KB of size to your app's classes.dex file. These additions can quickly accumulate for complex systems or shared libraries. If possible, consider using the@IntDef annotation and ProGuard to strip enumerations out and convert them to integers. This type conversion preserves all of the type safety benefits of enums.

单个枚举会使应用的 classes.dex 文件增加大约 1.0 到 1.4 KB 的大小。这些增加的大小会快速累积,产生复杂的系统或共享库。如果可能,请考虑使用 @IntDef 注释和 ProGuard 移除枚举并将它们转换为整数。此类型转换可保留枚举的各种安全优势。

运行例子
  private void single() {
        //内部类
        SingleClass.getSingle().test();
        //枚举
        SingleEnumClass.INSTANCE.test();
        //饿汉
        SingleHungryClass.getSingleHungry().test();
        //懒汉
        SingleLazyClass.getSingleLazy().test();
  }
2019-07-31 14:52:02.593 13601-13601/com.design.patterns D/>>>: 内部类
2019-07-31 14:52:02.593 13601-13601/com.design.patterns D/>>>: 枚举
2019-07-31 14:52:02.593 13601-13601/com.design.patterns D/>>>: 饿汉
2019-07-31 14:52:02.593 13601-13601/com.design.patterns D/>>>: 懒汉
单例模式大概就介绍到这里了,如果有什么错误可以在下方的评论中指出。

相关文章

  • 单例模式Java篇

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

  • python中OOP的单例

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

  • 单例

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

  • 设计模式 - 单例模式

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

  • 设计模式

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

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

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

  • python 单例

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

  • 基础设计模式:单例模式+工厂模式+注册树模式

    基础设计模式:单例模式+工厂模式+注册树模式 单例模式: 通过提供自身共享实例的访问,单例设计模式用于限制特定对象...

  • 单例模式

    JAVA设计模式之单例模式 十种常用的设计模式 概念: java中单例模式是一种常见的设计模式,单例模式的写法...

  • 设计模式之单例模式

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

网友评论

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

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