设计模式-单例模式

作者: KevinLive | 来源:发表于2017-06-02 16:44 被阅读28次

原文地址:LoveDev

单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一,该模式确保一个类只有一个实例

单例模式单例模式

懒汉式,线程不安全

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

懒汉式使用懒加载模式,但是在多线程环境中调用 getInstance() 就会创建多个实例

懒汉式,线程安全

class SynchronizedLazySingleton {

    private static SynchronizedLazySingleton instance;
    private SynchronizedLazySingleton(){}
    
    //添加synchronized关键字
    public static synchronized SynchronizedLazySingleton getInstance() {
        if (instance == null) {
            instance = new SynchronizedLazySingleton();
        }
        return instance;
    }
}

getInstance() 方法加上 synchronized 关键字实现线程安全,但是该方法效率上有问题,任何时候只能有一个线程调用 getInstance() 获取实例,而且实例第一次创建之后就不需要同步操作

饿汉式,线程安全

class HungrySingleton {

    private static final HungrySingleton instance = new HungrySingleton();

    private HungrySingleton(){}
    public static HungrySingleton getInstance(){
        return instance;
    }
}

实例被 static final 修饰,类加载时就会初始化,但它不是懒加载模式,如果 HungrySingleton 实例的创建需要某个条件参数,这种写法就不能实现

双重检验锁

class DoubleCheckedSingleton {

    private volatile static DoubleCheckedSingleton instance;

    private DoubleCheckedSingleton() {}

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

为了避免在多线程环境中执行 instance = new DoubleCheckedSingleton() 造成指令重排序,添加 volatile 关键字修饰,禁止指令重排序优化,抛开代码可读性来说这种写法已经完美了,但是这样你就满足了吗?没有的话就接着看下去吧

静态内部类

class StaticSingleton {

    private static class SingletonHolder {
        private static final StaticSingleton INSTANCE = new StaticSingleton();
    }
    private StaticSingleton (){}
    public static final StaticSingleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

该方法为 《Effective Java》上推荐的,它是懒汉式,并且性能没有缺陷

枚举

enum  EnumSingleton {
    INSTANCE
}

最简单的单例类,没有之一,还能防止序列化导致重新创建新的对象

思考

以上介绍的单例模式的写法均在单一进程中有效,如果是多进程环境中,这些写法均不能保证实例的唯一性,如何在多进程环境中保证单例模式的特性呢?

相关文章

  • 单例模式Java篇

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

  • python中OOP的单例

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

  • 单例

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

  • 设计模式 - 单例模式

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

  • 设计模式

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

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

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

  • python 单例

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

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

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

  • 单例模式

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

  • 设计模式之单例模式

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

网友评论

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

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