美文网首页
设计模式之01:单例模式

设计模式之01:单例模式

作者: 木木禾木 | 来源:发表于2017-05-07 10:57 被阅读0次

单例模式可以避免多个对象对资源的消耗。

使用场景

当某类只需一个对象的时候,如:IO的访问;数据库的访问;图片加载ImageLoader。

为什么ImageLoader要使用单例?
ImageLoader中又含有线程池、缓存、网络请求等比较耗资源的对象,如果多次构建,则会消耗过多资源。

实现方式

1. 声明式:不推荐
    private Test() {
    }

    public final static Test test = new Test();

优点:简单
缺点:粗暴(系统启动时初始化,对节约资源不够充分)

2. 懒汉式:不推荐
    private Test() {
    }

    private static Test test;

    public static synchronized Test getInstance() {
        if (test == null) {
            test = new Test();
        }
        return test;
    }

优点:第一次使用时候初始化,保证线程安全
缺点:每次使用都会同步,性能差

3. DCL(Double Check Lock)方式:可用,最常用的方式。
    private Test() {
    }

    private static Test test;

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

优点:第一次使用时初始化,保证线程安全,避免不必要的同步
缺点:高并发环境有缺陷

4. 静态内部类式:推荐。
    private Test() {
    }

    public static Test getInstance() {
        return InstanceHolder.test;
    }

    private static class InstanceHolder {
        private final static Test test = new Test();
    }

优点:延迟实例化,线程安全,单例唯一性。
缺点:class个数增加。

5. 枚举式
public enum Test {

    Instance;

    private int count;

    public void countPlus(int plus) {
        count += plus;
    }

}

使用:Test.Instance.countPlus(3);
优点:线程安全,绝对唯一性(上述的几种方式,在反序列化情况下会重新创建对象)。
缺点:没有延迟加载。

6. 容器式
    private static Map<String, Object> instanceMap = new HashMap<>();

    public static void registerService(String key, Object instance) {
        if (!instanceMap.containsKey(key)) {
            instanceMap.put(key, instance);
        }
    }

    public static Object getService(String key) {
        return instanceMap.get(key);
    }

系统启动时,将多种单例注入容器,使用时取出。

总结

单例的核心都是将构造函数私有化,通过静态方法获取唯一实例,尽量节约资源提高性能,保证线程安全,防止反序列化导致重新实例化。

相关文章

  • python中OOP的单例

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

  • 单例

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

  • python 单例

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

  • 单例模式

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

  • JAVA设计模式之单例模式

    JAVA设计模式之单例模式

  • Android 设计模式之简单工厂模式

    设计模式系列文章 Android 设计模式之单例模式 Android 设计模式之Builder模式 Android...

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

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

  • iOS模式设计之--创建型:1、单例模式

    iOS模式设计之--1、单例模式

  • 单例模式Java篇

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

  • 设计模式学习之单例模式

    设计模式之单例模式 1 什么是单例模式 在维基百科中单例模式定义为 单例模式,也叫单子模式,是一种常用的软件设计模...

网友评论

      本文标题:设计模式之01:单例模式

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