设计模式-单例模式

作者: 编码工匠 | 来源:发表于2018-08-28 18:58 被阅读0次

1.懒汉式-线程不安全

public class SingletonLazyNoSafe {
    private static SingletonLazyNoSafe singletonLazyNoSafe;

    private SingletonLazyNoSafe() {}

    public static SingletonLazyNoSafe getInstance(){
        if (singletonLazyNoSafe ==null){
            singletonLazyNoSafe = new SingletonLazyNoSafe();
        }
        return singletonLazyNoSafe;
    }
}

2.懒汉式-线程安全

public class SingletonLazySafe {
    private static SingletonLazySafe singletonLazySafe;
    private SingletonLazySafe(){
    }
    public synchronized static SingletonLazySafe getInstance(){
        if (singletonLazySafe == null){
            singletonLazySafe = new SingletonLazySafe();
        }
        return singletonLazySafe;
    }
}

3.饿汉式-线程安全

public class SingletonHungerSafe {
    private static SingletonHungerSafe singletonHungerSafe = new SingletonHungerSafe();
    private SingletonHungerSafe(){}

    public static SingletonHungerSafe getInstance(){
        return singletonHungerSafe;
    }
}

4.饿汉式-静态代码块-线程安全

public class SingletonHungerStaticBlockSafe {
    private static SingletonHungerStaticBlockSafe singletonHungerStaticBlockSafe;
    static {
        singletonHungerStaticBlockSafe = new SingletonHungerStaticBlockSafe();
    }
    private SingletonHungerStaticBlockSafe(){}

    public static SingletonHungerStaticBlockSafe getInstance(){
        return singletonHungerStaticBlockSafe;
    }
}

5.懒汉式-静态内部类-线程安全

public class SingletonStaticInnerClass {
    private SingletonStaticInnerClass(){}
    private static class InstanceSingleton{
        public static final SingletonStaticInnerClass SINGLETON_STATIC_INNER_CLASS = new SingletonStaticInnerClass();
    }
    public static SingletonStaticInnerClass getInstance(){
        return InstanceSingleton.SINGLETON_STATIC_INNER_CLASS;
    }
}

6.懒汉式-双重校验锁-线程安全

public class SingletonDoubleVerifyLock {
    private static SingletonDoubleVerifyLock singletonDoubleVerifyLock;
    private SingletonDoubleVerifyLock(){}

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

7.懒汉式-枚举实现-线程安全(推荐)

public enum SingletonEnum {
    INSTANCE;
    SingletonEnum() {}
}

相关文章

  • 单例模式Java篇

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

  • python中OOP的单例

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

  • 单例

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

  • 设计模式 - 单例模式

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

  • 设计模式

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

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

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

  • python 单例

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

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

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

  • 单例模式

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

  • 设计模式之单例模式

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

网友评论

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

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