美文网首页
单例模式

单例模式

作者: CZKGO | 来源:发表于2018-09-04 16:35 被阅读0次

简介

    单例模式是比较常用的设计模式,属于创建者模式。其主要用来解决一个全局类的频繁创建和销毁,节省系统资源。

特点

  • 单例模式的类只有一个实例
  • 单例模式的实例有他自己创建
  • 单例模式向整个系统提供这个唯一实例

实现

懒汉式-慎用

public class SingleInstance {

    private static SingleInstance instance;

    private SingleInstance() {}

    public static SingleInstance getInstance() {
        if (instance == null) {
            instance = new SingleInstance();
        }
        return instance;
    }
}

最基本实现方式,不支持多线程,多线程下可能同时实例化产生多个实例,所以严格意义上不算单例模式。

懒汉式(同步方法)-不推荐

public class SingleInstance {

    private static SingleInstance instance;

    private SingleInstance() {}

    public static synchronized SingleInstance getInstance() {
        if (instance == null) {
            instance = new SingleInstance();
        }
        return instance;
    }
}
  • 优点:在基本的懒汉式下实现了线程安全。
  • 缺点:必须加锁 synchronized 才能保证单例,但加锁会影响效率(关于影响效率查看synchronized的实现机制)。

饿汉式(静态常量)-不推荐

public class SingleInstance {

    private final static SingleInstance INSTANCE = new SingleInstance();

    private SingleInstance(){}

    public static SingleInstance getInstance(){
        return INSTANCE;
    }
}
  • 优点:写法简单,在类装载的时完成实例化。避免线程同步问题。
  • 缺点:在类装载时完成实例化,如果从未使用过这个实例,会造成内存浪费。

饿汉式(静态代码块)-不推荐

public class SingleInstance {

    private static SingleInstance instance;

    static {
        instance = new SingleInstance();
    }

    private SingleInstance() {}

    public SingleInstance getInstance() {
        return instance;
    }
}

该方法和静态常量形式的饿汉式类似,优缺点一致。

双重校验锁

public class SingleInstance {

    private static volatile SingleInstance instance;

    private SingleInstance() {}

    public static SingleInstance getInstance() {
        if (instance == null) {
            synchronized (SingleInstance.class) {
                if (instance == null) {
                    instance = new SingleInstance();
                }
            }
        }
        return instance;
    }
}
  • 优点:两次if (instance == null)检查保证线程安全;延迟加载;效率较高。

静态内部类

public class SingleInstance {

    private SingleInstance() {}

    private static class Instance {
        private static final SingleInstance INSTANCE = new SingleInstance();
    }

    public static SingleInstance getInstance() {
        return Instance.INSTANCE;
    }
}

类的静态属性只会在第一次加载类的时候初始化,所以在这里,JVM帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。

  • 优点:线程安全,延迟加载,效率高。

枚举

public enum SingleInstance {
    INSTANCE;
    public void whateverMethod() {

    }
}

借助JDK1.5中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象

相关文章

  • 【设计模式】单例模式

    单例模式 常用单例模式: 懒汉单例模式: 静态内部类单例模式: Android Application 中使用单例模式:

  • Android设计模式总结

    单例模式:饿汉单例模式://饿汉单例模式 懒汉单例模式: Double CheckLock(DCL)实现单例 Bu...

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

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

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

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

  • Telegram开源项目之单例模式

    NotificationCenter的单例模式 NotificationCenter的单例模式分析 这种单例模式是...

  • 单例模式Java篇

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

  • IOS单例模式的底层原理

    单例介绍 本文源码下载地址 1.什么是单例 说到单例首先要提到单例模式,因为单例模式是单例存在的目的 单例模式是一...

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • 单例模式

    单例模式1 单例模式2

  • java的单例模式

    饿汉单例模式 懒汉单例模式

网友评论

      本文标题:单例模式

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