美文网首页
单例模式的写法

单例模式的写法

作者: 皮皮咕 | 来源:发表于2018-08-01 14:36 被阅读0次

普通懒汉模式--非线程安全

public class SingletonExample1 {
    // 私有构造函数
    private SingletonExample1() {}
    // 单例对象
    private static SingletonExample1 instance = null;
    // 静态的工厂方法
    public static SingletonExample1 getInstance() {
        if (instance == null) {
            instance = new SingletonExample1();
        }
        return instance;
    }
}

普通饿汉模式--线程安全,但是若初始化过程比较复杂,则可能会浪费资源

public class SingletonExample2 {
    // 私有构造函数
    private SingletonExample2() {}
    // 单例对象
    private static SingletonExample2 instance = new SingletonExample2();
    // 静态的工厂方法
    public static SingletonExample2 getInstance() {
        return instance;
    }
}

加synchronized的懒汉模式--线程安全,但是加锁使得访问的速度变慢

public class SingletonExample3 {
    // 私有构造函数
    private SingletonExample3() {}
    // 单例对象
    private static SingletonExample3 instance = null;
    // 静态的工厂方法
    public static synchronized SingletonExample3 getInstance() {
        if (instance == null) {
            instance = new SingletonExample3();
        }
        return instance;
    }
}

双重同步锁懒汉模式--非线程安全

public class SingletonExample4 {
    // 私有构造函数
    private SingletonExample4() {}
    // 单例对象
    private static SingletonExample4 instance = null;
    // 静态的工厂方法
    public static SingletonExample4 getInstance() {
        if (instance == null) { 
            synchronized (SingletonExample4.class) { 
                if (instance == null) {
                    // memory = allocate(); //1.分配对象内存空间
                    // instance(memory);    //2.初始化对象
                    // instance = memory;   //3.设置instance指向刚分配的内存地址,此时instance!=null
                    // 指令重排机制会使该方法出现线程问题
                    instance = new SingletonExample4();
                }
            }
        }
        return instance;
    }
}

加volatile双重同步锁懒汉模式--线程安全

public class SingletonExample4 {
    // 私有构造函数
    private SingletonExample4() {}
    // 单例对象,volatile禁止指令重排
    private volatile static SingletonExample4 instance = null;
    // 静态的工厂方法
    public static SingletonExample4 getInstance() {
        if (instance == null) { 
            synchronized (SingletonExample4.class) { 
                if (instance == null) {
                    instance = new SingletonExample4();
                }
            }
        }
        return instance;
    }
}

枚举--推荐使用

public class SingletonExample7 {
    // 私有构造函数
    private SingletonExample7() {}
    // 获取实例
    public static SingletonExample7 getInstance() {
        return Singleton.INSTANCE.getInstance();
    }
    // 枚举
    private enum Singleton {
        INSTANCE;
        private SingletonExample7 singleton;
        // JVM保证这个方法绝对只调用一次
        Singleton() {
            singleton = new SingletonExample7();
        }
        public SingletonExample7 getInstance() {
            return singleton;
        }
    }
}

相关文章

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

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

  • 第03条 用私有构造方法或者枚举类型强化Singleton属性

    单例模式最佳写法1 - 双重校验锁 单例模式最佳写法2 - 静态内部类

  • 单例模式

    单例模式的写法

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • Kotlin中的单例模式与Java对比

    目前java中的单例模式有多种写法,kotlin中的写法更多一点,本篇会总结全部的到单例模式写法。 一、懒人写法(...

  • 单例模式

    一、介绍 二、单例模式代码实现 三、单例的简介写法

  • java 24 设计模式

    单例模式java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例、饿汉式单例、...

  • Android中常见的内存泄漏汇总

    1.单例模式的错误写法 单例模式的正确写法: 2.非静态内部类创建静态实例造成的内存泄漏错误写法 正确写法:将该内...

  • 单例模式

    单例模式--概念 单例模式常见的写法有:懒汉式,饿汉式,登记式。单例模式的特点有:1.单例类只能有1个实例2.单例...

  • 单例的2种写法

    单例模式是iOS开发中最常用的设计模式,iOS的单例模式有两种官方写法,如下: 1,常用写法 import "Se...

网友评论

      本文标题:单例模式的写法

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