zoukankan      html  css  js  c++  java
  • 单例设计模式

    1. 介绍

    所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)

    单例设计模式分类两种:

    • 饿汉式:类加载就会导致该单实例对象被创建

    • 懒汉式:类加载不会导致该单实例对象被创建,而是首次使用该对象时才会创建


    2. 饿汉式(静态常量)

    package com.atguigu.singleton.type1;
    
    /**
     * @author john
     * @date 2021/1/13 - 9:43
     */
    public class Singleton {
        // 1. 构造器私有化,外部能new
        private Singleton() {
        }
    
        //2. 本类内部创建对象实例
        private final static Singleton instance = new Singleton();
    
        // 3. 提供一个公有的静态方法,返回实例对象
        public static Singleton getInstance() {
            return instance;
        }
        
    }
    
    
    package com.atguigu.singleton.type1;
    
    /**
     * @author john
     * @date 2021/1/13 - 9:43
     */
    public class SingletonTest01 {
        public static void main(String[] args) {
            Singleton instance = Singleton.getInstance();
            Singleton instance2 = Singleton.getInstance();
            System.out.println(instance == instance2);
    
            System.out.println("instance.hashCode() = " + instance.hashCode());
            System.out.println("instance2.hashCode() = " + instance2.hashCode());
        }
    }
    
    

    该方式在成员位置声明Singleton类型的静态变量,并创建Singleton类的对象instance。instance对象是随着类的加载而创建的。如果该对象足够大的话,而一直没有使用就会造成内存的浪费。


    3. 饿汉式(静态代码块)

    package com.atguigu.singleton.type2;
    
    /**
     * @author john
     * @date 2021/1/13 - 10:10
     */
    public class Singleton {
        // 1. 构造器私有化,外部能new
        private Singleton() {
        }
    
        // 2. 本类内部创建对象实例
        private static Singleton instance;
    
        static { // 在静态代码块中,创建单例对象
            instance = new Singleton();
        }
    
        // 3. 提供一个公有的静态方法,返回实例对象
        public static Singleton getInstance() {
            return instance;
        }
    }
    
    
    package com.atguigu.singleton.type2;
    
    
    /**
     * @author john
     * @date 2021/1/13 - 9:43
     */
    public class SingletonTest02 {
        public static void main(String[] args) {
            Singleton instance = Singleton.getInstance();
            Singleton instance2 = Singleton.getInstance();
            System.out.println(instance == instance2);
    
            System.out.println("instance.hashCode() = " + instance.hashCode());
            System.out.println("instance2.hashCode() = " + instance2.hashCode());
        }
    }
    
    

    该方式在成员位置声明Singleton类型的静态变量,而对象的创建是在静态代码块中,也是对着类的加载而创建。所以和饿汉式的方式1基本上一样,当然该方式也存在内存浪费问题。


    4. 懒汉式(线程不安全)

    package com.atguigu.singleton.type3;
    
    /**
     * @author john
     * @date 2021/1/13 - 10:19
     */
    public class Singleton {
        // 在成员位置创建该类的对象
        private static Singleton instance;
    
        // //私有构造方法
        private Singleton() {
        }
    
        // 提供一个静态的公有方法,当使用到该方法时,才去创建instance
        // 即懒汉式
    
        // 对外提供静态方法获取该对象
        public static Singleton getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
    
    
    package com.atguigu.singleton.type3;
    
    
    /**
     * @author john
     * @date 2021/1/13 - 10:19
     */
    public class SingletonTest03 {
        public static void main(String[] args) {
            Singleton instance = Singleton.getInstance();
            Singleton instance2 = Singleton.getInstance();
            System.out.println(instance == instance2);
    
            System.out.println("instance.hashCode() = " + instance.hashCode());
            System.out.println("instance2.hashCode() = " + instance2.hashCode());
        }
    }
    
    

    从上面代码我们可以看出该方式在成员位置声明Singleton类型的静态变量,并没有进行对象的赋值操作,那么什么时候赋值的呢?当调用getInstance()方法获取Singleton类的对象的时候才创建Singleton类的对象,这样就实现了懒加载的效果。但是,如果是多线程环境,会出现线程安全问题。


    5. 懒汉式(线程安全,同步方法)

    package com.atguigu.singleton.type4;
    
    /**
     * @author john
     * @date 2021/1/13 - 10:24
     */
    public class Singleton {
        //在成员位置创建该类的对象
        private static Singleton instance;
    
        //私有构造方法
        private Singleton() {
        }
    
        // 提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
        // 即懒汉式
    
        //对外提供静态方法获取该对象
        public static synchronized Singleton getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
    
    
    package com.atguigu.singleton.type4;
    
    
    /**
     * @author john
     * @date 2021/1/13 - 10:27
     */
    public class SingeltonTest04 {
        public static void main(String[] args) {
            Singleton instance = Singleton.getInstance();
            Singleton instance2 = Singleton.getInstance();
            System.out.println(instance == instance2);
    
            System.out.println("instance.hashCode() = " + instance.hashCode());
            System.out.println("instance2.hashCode() = " + instance2.hashCode());
        }
    }
    
    

    该方式也实现了懒加载效果,同时又解决了线程安全问题。但是在getInstance()方法上添加了synchronized关键字,导致该方法的执行效果特别低。从上面代码我们可以看出,其实就是在初始化instance的时候才会出现线程安全问题,一旦初始化完成就不存在了。


    6. 懒汉式(线程安全,同步代码块)

    package com.atguigu.singleton.type05;
    
    import java.lang.reflect.InaccessibleObjectException;
    
    /**
     * @author john
     * @date 2021/1/13 - 10:29
     */
    public class Singleton {
        private static Singleton instance;
    
        private Singleton() {
        }
    
        public static Singleton getInstance() {
            if (instance == null) {
                synchronized (Singleton.class) {
                    instance = new Singleton();
                }
            }
            return instance;
        }
    }
    
    

    不推荐

    7. 双重检查(推荐)

    双重检查锁模式是一种非常好的单例实现模式,解决了单例、性能、线程安全问题,上面的双重检测锁模式看上去完美无缺,其实是存在问题,在多线程的情况下,可能会出现空指针问题,出现问题的原因是JVM在实例化对象的时候会进行优化和指令重排序操作。

    要解决双重检查锁模式带来空指针异常的问题,只需要使用 volatile 关键字, volatile 关键字可以保证可见性和有序性。

    package com.atguigu.singleton.type06;
    
    /**
     * @author john
     * @date 2021/1/13 - 10:33
     */
    public class Singleton {
        // 解决双重检查锁模式带来空指针异常的问题,只需要使用 `volatile` 关键字, `volatile` 关键字可以保证可见性和有序性。
        private static volatile Singleton instance;
    
        //私有构造方法
        private Singleton() {
        }
    
        // 提供一个静态的公有方法,加入双重检查代码,解决线程安全问题,同时解决懒加载问题
        // 同时保证了效率,推荐使用
    
        //对外提供静态方法获取该对象
        public static synchronized Singleton getInstance() {
            //第一次判断,如果instance不为null,不进入抢锁阶段,直接返回实例
            if (instance == null) {
                synchronized (Singleton.class) {
                    //抢到锁之后再次判断是否为null
                    if (instance == null) {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
    
    
    package com.atguigu.singleton.type06;
    
    /**
     * @author john
     * @date 2021/1/13 - 10:38
     */
    public class SingletonTest06 {
        public static void main(String[] args) {
            System.out.println("双重检查");
            Singleton instance = Singleton.getInstance();
            Singleton instance2 = Singleton.getInstance();
            System.out.println(instance == instance2);
            System.out.println("instance.hashCode() = " + instance.hashCode());
            System.out.println("instance2.hashCode() = " + instance2.hashCode());
        }
    }
    
    

    添加 volatile 关键字之后的双重检查锁模式是一种比较好的单例实现模式,能够保证在多线程的情况下线程安全也不会有性能问题。


    8. 静态内部类(推荐)

    静态内部类单例模式中实例由内部类创建,由于 JVM 在加载外部类的过程中, 是不会加载静态内部类的, 只有内部类的属性/方法被调用时才会被加载, 并初始化其静态属性。静态属性由于被 static 修饰,保证只被实例化一次,并且严格保证实例化顺序。

    package com.atguigu.singleton.type7;
    
    /**
    静态内部类方式
     * @author john
     * @date 2021/1/13 - 18:46
     */
    public class Singleton {
        private static volatile Singleton instance;
    
        // 构造器私有化
        private Singleton() {
        }
    
        // 写一个静态内部类,该类中有一个静态属性 Singleton
        private static class SingleInstance {
            private static final Singleton INSTANCE = new Singleton();
        }
    
        // 提供一个静态的公有方法,直接返回SingleInstance.INSTANCE
        public static synchronized Singleton getInstance() {
            return SingleInstance.INSTANCE;
        }
    }
    
    
    package com.atguigu.singleton.type7;
    
    
    /**
     * @author john
     * @date 2021/1/13 - 18:49
     */
    public class SingletonTest7 {
        public static void main(String[] args) {
            System.out.println("使用静态内部类完成单例模式");
            Singleton instance = Singleton.getInstance();
            Singleton instance2 = Singleton.getInstance();
            System.out.println(instance == instance2);
            System.out.println("instance.hashCode() = " + instance.hashCode());
            System.out.println("instance2.hashCode() = " + instance2.hashCode());
        }
    }
    
    

    第一次加载Singleton类时不会去初始化INSTANCE,只有第一次调用getInstance,虚拟机加载SingletonHolder

    并初始化INSTANCE,这样不仅能确保线程安全,也能保证 Singleton 类的唯一性。

    静态内部类单例模式是一种优秀的单例模式,是开源项目中比较常用的一种单例模式。在没有加任何锁的情况下,保证了多线程下的安全,并且没有任何性能影响和空间的浪费。


    9. 枚举(推荐)

    枚举类实现单例模式是极力推荐的单例实现模式,因为枚举类型是线程安全的,并且只会装载一次,设计者充分的利用了枚举的这个特性来实现单例模式,枚举的写法非常简单,而且枚举类型是所用单例实现中唯一一种不会被破坏的单例实现模式。

    package com.atguigu.singleton.type8;
    
    /**
     * @author john
     * @date 2021/1/13 - 18:58
     */
    public enum Singleton {
        INSTANCE; // 属性
    
        public void sayOK() {
            System.out.println("ok~~");
        }
    }
    
    
    package com.atguigu.singleton.type8;
    
    /**
     * @author john
     * @date 2021/1/13 - 18:59
     */
    public class SingletonTest8 {
        public static void main(String[] args) {
            Singleton instance = Singleton.INSTANCE;
            Singleton instance2 = Singleton.INSTANCE;
            System.out.println(instance == instance2);
    
            System.out.println("instance.hashCode() = " + instance.hashCode());
            System.out.println("instance2.hashCode() = " + instance2.hashCode());
            instance.sayOK();
        }
    }
    
    


    10. 存在的问题

    1. 问题演示

    破坏单例模式:

    使上面定义的单例类(Singleton)可以创建多个对象,枚举方式除外。有两种方式,分别是序列化和反射。

    • 序列化反序列化

      Singleton类:

      public class Singleton implements Serializable {
      
          //私有构造方法
          private Singleton() {}
      
          private static class SingletonHolder {
              private static final Singleton INSTANCE = new Singleton();
          }
      
          //对外提供静态方法获取该对象
          public static Singleton getInstance() {
              return SingletonHolder.INSTANCE;
          }
      }
      

      Test类:

      public class Test {
          public static void main(String[] args) throws Exception {
              //往文件中写对象
              //writeObject2File();
              //从文件中读取对象
              Singleton s1 = readObjectFromFile();
              Singleton s2 = readObjectFromFile();
      
              //判断两个反序列化后的对象是否是同一个对象
              System.out.println(s1 == s2);
          }
      
          private static Singleton readObjectFromFile() throws Exception {
              //创建对象输入流对象
              ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\Users\Think\Desktop\a.txt"));
              //第一个读取Singleton对象
              Singleton instance = (Singleton) ois.readObject();
      
              return instance;
          }
      
          public static void writeObject2File() throws Exception {
              //获取Singleton类的对象
              Singleton instance = Singleton.getInstance();
              //创建对象输出流
              ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\Users\Think\Desktop\a.txt"));
              //将instance对象写出到文件中
              oos.writeObject(instance);
          }
      }
      

      上面代码运行结果是false,表明序列化和反序列化已经破坏了单例设计模式。

    • 反射

      Singleton类:

      public class Singleton {
      
          //私有构造方法
          private Singleton() {}
          
          private static volatile Singleton instance;
      
          //对外提供静态方法获取该对象
          public static Singleton getInstance() {
      
              if(instance != null) {
                  return instance;
              }
      
              synchronized (Singleton.class) {
                  if(instance != null) {
                      return instance;
                  }
                  instance = new Singleton();
                  return instance;
              }
          }
      }
      

      Test类:

      public class Test {
          public static void main(String[] args) throws Exception {
              //获取Singleton类的字节码对象
              Class clazz = Singleton.class;
              //获取Singleton类的私有无参构造方法对象
              Constructor constructor = clazz.getDeclaredConstructor();
              //取消访问检查
              constructor.setAccessible(true);
      
              //创建Singleton类的对象s1
              Singleton s1 = (Singleton) constructor.newInstance();
              //创建Singleton类的对象s2
              Singleton s2 = (Singleton) constructor.newInstance();
      
              //判断通过反射创建的两个Singleton对象是否是同一个对象
              System.out.println(s1 == s2);
          }
      }
      

      上面代码运行结果是false,表明序列化和反序列化已经破坏了单例设计模式

    注意:枚举方式不会出现这两个问题。

    2 问题的解决

    • 序列化、反序列方式破坏单例模式的解决方法

      在Singleton类中添加readResolve()方法,在反序列化时被反射调用,如果定义了这个方法,就返回这个方法的值,如果没有定义,则返回新new出来的对象。

      Singleton类:

      public class Singleton implements Serializable {
      
          //私有构造方法
          private Singleton() {}
      
          private static class SingletonHolder {
              private static final Singleton INSTANCE = new Singleton();
          }
      
          //对外提供静态方法获取该对象
          public static Singleton getInstance() {
              return SingletonHolder.INSTANCE;
          }
          
          /**
           * 下面是为了解决序列化反序列化破解单例模式
           */
          private Object readResolve() {
              return SingletonHolder.INSTANCE;
          }
      }
      

      源码解析:

      ObjectInputStream类

      public final Object readObject() throws IOException, ClassNotFoundException{
          ...
          // if nested read, passHandle contains handle of enclosing object
          int outerHandle = passHandle;
          try {
              Object obj = readObject0(false);//重点查看readObject0方法
          .....
      }
          
      private Object readObject0(boolean unshared) throws IOException {
      	...
          try {
      		switch (tc) {
      			...
      			case TC_OBJECT:
      				return checkResolve(readOrdinaryObject(unshared));//重点查看readOrdinaryObject方法
      			...
              }
          } finally {
              depth--;
              bin.setBlockDataMode(oldMode);
          }    
      }
          
      private Object readOrdinaryObject(boolean unshared) throws IOException {
      	...
      	//isInstantiable 返回true,执行 desc.newInstance(),通过反射创建新的单例类,
          obj = desc.isInstantiable() ? desc.newInstance() : null; 
          ...
          // 在Singleton类中添加 readResolve 方法后 desc.hasReadResolveMethod() 方法执行结果为true
          if (obj != null && handles.lookupException(passHandle) == null && desc.hasReadResolveMethod()) {
          	// 通过反射调用 Singleton 类中的 readResolve 方法,将返回值赋值给rep变量
          	// 这样多次调用ObjectInputStream类中的readObject方法,继而就会调用我们定义的readResolve方法,所以返回的是同一个对象。
          	Object rep = desc.invokeReadResolve(obj);
           	...
          }
          return obj;
      }
      
    • 反射方式破解单例的解决方法

      public class Singleton {
      
          //私有构造方法
          private Singleton() {
              /*
                 反射破解单例模式需要添加的代码
              */
              if(instance != null) {
                  throw new RuntimeException();
              }
          }
          
          private static volatile Singleton instance;
      
          //对外提供静态方法获取该对象
          public static Singleton getInstance() {
      
              if(instance != null) {
                  return instance;
              }
      
              synchronized (Singleton.class) {
                  if(instance != null) {
                      return instance;
                  }
                  instance = new Singleton();
                  return instance;
              }
          }
      }
      

      说明:

      这种方式比较好理解。当通过反射方式调用构造方法进行创建创建时,直接抛异常。不运行此中操作。

  • 相关阅读:
    数据结构—链表
    python字母串查找基本操作
    python九九乘法表程序代码
    SpringMVC跨域问题排查以及源码实现
    深入理解MySql子查询IN的执行和优化
    Dubbo源码阅读-服务导出
    Disconf源码分析之启动过程分析下(2)
    Disconf源码分析之启动过程分析上(1)
    Java多线程volatile和synchronized总结
    Java多线程基础总结
  • 原文地址:https://www.cnblogs.com/ifme/p/14274401.html
Copyright © 2011-2022 走看看