美文网首页
单例模式

单例模式

作者: isjinhao | 来源:发表于2018-07-19 15:54 被阅读0次

定义

  一个类至多有一个对象实例。这个实例被称为单例。在使用时才初始化被称为懒汉式单例,类一加载就初始化被叫做饿汉式单例。有8种常见实现方式。

实现

  1. 静态常量法:可用
public class Singleton {
    private final static Singleton instance = new Singleton();
    private Singleton(){}
    public static Singleton getInstance(){
        return instance;
    }
}
  1. 静态代码块:可用
public class Singleton {
    private static Singleton instance;
    static { instance = new Singleton(); }
    private Singleton() {}
    public Singleton getInstance() {
        return instance;
        }
}
  1. 静态方法:不可用
public class Singleton {
    private static Singleton singleton;
    private Singleton() {}
    public static Singleton getInstance() {
        if (singleton == null) 
            singleton = new Singleton();
        return singleton;
    }
}

  线程A进入第7句但还未来得及初始化时,线程B在第5句判断为null,所以也可以进入第6句,有并发问题。

  1. 同步方法:可用但不推荐
public class Singleton {
    private static Singleton singleton;
    private Singleton() {}
    public static synchronized Singleton getInstance() {
        if (singleton == null)
            singleton = new Singleton();
        return singleton;
    }
}
  1. 同步代码块:不可用
public class Singleton {
    private static Singleton singleton;
    private Singleton() {}
    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                singleton = new Singleton();
            }
        }
        return singleton;
    }
}

  线程A进入第7句但还未来得及初始化时,线程B在第5句判断为null,虽然得等到A初始化完成,但也可以进入第7句,有并发问题。

  1. 双重检查:可用且推荐用
public class Singleton {
    private static volatile Singleton singleton;
    private Singleton() {}
    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
  1. 静态内部类:可用且推荐用
public class Singleton {
    private Singleton() {}
    private static class SingletonInstance {
        private static final Singleton INSTANCE = new Singleton();
    }
    public static Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }
}
  1. 枚举:可用且推荐用
public enum Singleton {
    INSTANCE;
    public void whateverMethod() {
        doSomethingHere...
    }
}

原文来至https://www.cnblogs.com/zhaoyan001/p/6365064.html

相关文章

  • 【设计模式】单例模式

    单例模式 常用单例模式: 懒汉单例模式: 静态内部类单例模式: 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/rfkrpftx.html