单例模式设计,是采取一定的方法,使得在整个软件系统中,对某个类只能存在一个对象实例,并且这个类只提供一个取得其对象实例的方法。

单例模式注意事项和细节说明

  1. 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统的性能。
  2. 当我们想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是使用new

单例模式使用的场景:需要频繁的进行创建和销毁的对象,创建对象时消耗时间过多或者耗费资源过多,但又经常用到的对象、工具类对象,频繁访问数据库或文件的对象(比如数据源,session工厂等)。

单例模式有八种方式:

  1. 饿汉式(静态常量)
  2. 饿汉式(静态代码块)
  3. 懒汉式(线程不安全)
  4. 懒汉式(线程安全,同步方法)
  5. 懒汉式(线程安全,同步代码块)
  6. 双重检查
  7. 静态内部类
  8. 枚举

饿汉式(不推荐使用)

饿汉式(静态常量)实例:

  1. 构造器私有化
  2. 类内部创建对象
  3. 向外暴露一个静态的公有方法,返回实例
    1. class Singleton{
    2. //构造器私有化
    3. private Singleton(){}
    4. //在类内部创建对象实例
    5. private final static Singleton instance = new Singleton();
    6. //提供一个公有的静态方法,返回实例对象
    7. public static Singleton getInstance(){
    8. return instance;
    9. }
    10. }
    11. public class SingletonTest01 {
    12. public static void main(String[] args) {
    13. Singleton instance = Singleton.getInstance();
    14. Singleton instance2 = Singleton.getInstance();
    15. System.out.println(instance == instance2); //true
    16. System.out.println(instance.hashCode() == instance2.hashCode()); //true
    17. }
    18. }
    饿汉式(静态代码块):
    1. class Singleton{
    2. //构造器私有化
    3. private Singleton(){}
    4. private static Singleton instance;
    5. //在类内部创建对象实例
    6. static {
    7. instance = new Singleton();
    8. }
    9. //提供一个公有的静态方法,返回实例对象
    10. public static Singleton getInstance(){
    11. return instance;
    12. }
    13. }

分析:这种两种写法简单,在类加载的时候就完成了实例化,避免了线程同步问题。但是没有实现lazy loading的效果,可能造成内存浪费。

懒汉式(不推荐使用)

懒汉式(线程不安全):

  1. class Singleton{
  2. //构造器私有化
  3. private Singleton(){}
  4. private static Singleton instance;
  5. // 提供一个公有的静态方法
  6. // 只有在使用到该方法的时候,才去创建instace
  7. public static Singleton getInstance(){
  8. if(instance == null){
  9. instance = new Singleton();
  10. }
  11. return instance;
  12. }
  13. }

分析:起到了lazy loading的效果,但是只能在单线程下使用。如果在多线程的时候,一个线程进入了**if(singleton == null)**判断语句块,还没有来的及执行,其他的线程也通过了这个判断语句,就会产生多个实例。所以,开发中不要用这种方式。

懒汉式(线程安全,同步方法或同步代码块):
使用同步方法或者同步代码块,解决了线程安全问题,但是每个线程在想要获得类的实例的时候,执行getInstance()方法都要进行同步,效率太低。所以也不推荐使用。

  1. //使用同步方法
  2. public static synchronized Singleton getInstance(){
  3. if(instance == null){
  4. instance = new Singleton();
  5. }
  6. return instance;
  7. }
  8. public static Singleton getInstance(){
  9. if(instance == null){
  10. //使用同步代码块
  11. synchronized(Singleton.class){
  12. instance = new Singleton();
  13. }
  14. instance = new Singleton();
  15. }
  16. return instance;
  17. }

双重检查

//双重检查
class Singleton{
    private static volatile Singleton instance;
    private Singleton(){}
    //提供了一个静态的公有方法,加入双重检查代码,解决线程安全问题,同时解决懒加载问题。
    //保证了效率,推荐使用
    public static synchronized Singleton getInstance(){
        if(instance == null){
            synchronized(Singleton.class){
                if(instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

分析:进行了两次getInstance()检查,可以保证线程安全。实例化代码只用执行一次,后面再进行访问的时候,直接return实例化对象,也避免了反复进行方法同步。线程安全,延迟加载,效率较高,推荐使用。

静态内部类

class Singleton{
    private static volatile Singleton instance;
    private Singleton(){}
    //写一个静态内部类,该类中有一个静态属性Singleton
    private static class SingletonInstance{
        private static final Singleton INSTANCE = new Singleton();
    }
    //提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
    public static synchronized Singleton getInstance(){
        return SingletonInstance.INSTANCE;
    }
}

分析:

  • 静态内部类方式在Singleton类被装载时并不会立即实例化,而是在需要实例化的时候,调用getInstance方法,才会装载SingletonInstance类,从而完成Singleton的实例化。
  • 这种方式采用了类装载机制来保证初始化实例时只有一个线程,避免了线程不安全。推荐使用。


枚举

enum Singleton{
    INSTANCE; //属性
    public void sayOK(){
        System.out.println("OK");
    }

}
public class SingletonTest01 {
    public static void main(String[] args) {
        Singleton instance = Singleton.INSTANCE;
        Singleton instance2 = Singleton.INSTANCE;
        System.out.println(instance == instance2); //true
        System.out.println(instance.hashCode() == instance2.hashCode()); //true
        instance.sayOK();

    }
}

分析:借助枚举来实现单例模式,不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。推荐使用。