单例模式

单例模式

饿汉式

//饿汉式单例
public class Hungry {

   //可能浪费空间
   private byte[] data1 =new byte[1024*1024];
   private byte[] data2 =new byte[1024*1024];
   private byte[] data3 =new byte[1024*1024];
   private byte[] data4 =new byte[1024*1024];

   private Hungry(){
  }

   private final static Hungry HUNGRY=new Hungry();
   public static Hungry getInstance(){
       return HUNGRY;
  }
}

DCL懒汉式

package com.vogt.single;

import javafx.scene.control.Label;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
* @author VOGT
* @version V1.0
* @Package com.vogt.single
* @date 2020/4/13 10:34
*/
//懒汉式单例
public class LazyMan {
   public static boolean vogt=false;
   private LazyMan() {
       synchronized (LazyMan.class){
           if(vogt==false){
               vogt=true;
          }else {
               throw new RuntimeException("不要试图用反射破坏异常");
          }
      }
  }

   private volatile static LazyMan lazyMan;

   public static LazyMan getInstance() {
       //双重检测锁模式的懒汉式单例,DCL懒汉式
       if (lazyMan == null) {
           synchronized (LazyMan.class) {
               if (lazyMan == null) {
                   lazyMan = new LazyMan();//不是一个原子性操作
                   /*
                    * 1.分配内存空间
                    * 2.执行构造方法、初始化对象
                    * 3.把对象指向这个空间
                    *
                    * 123
                    * 132 A
                    *     B//可能lazyMan还没有完成构造
                    * */
              }
          }
      }
       return lazyMan;
  }
   //反射破解
   public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
       //LazyMan instance = LazyMan.getInstance();
       Field vogt = LazyMan.class.getDeclaredField("vogt");
       vogt.setAccessible(true);

       Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
       declaredConstructor.setAccessible(true);
       LazyMan instance = declaredConstructor.newInstance();

       vogt.set(instance,false);

       LazyMan instance2 = declaredConstructor.newInstance();

       System.out.println(instance);
       System.out.println(instance2);
  }

}
//
//   //单线程ok,多线程不行
//   public static void main(String[] args) {
//       for (int i = 0; i < 10; i++) {
//           new Thread(() -> {
//               LazyMan.getInstance();
//           }).start();
//       }
//
//
//   }
//}

静态内部类式

package com.vogt.single;

/**
* @author VOGT
* @version V1.0
* @Package com.vogt.single
* @date 2020/4/13 10:47
*/
//静态内部类
public class Holder {
   private Holder(){

  }

   public static Holder getInstance(){
       return InnereClass.HOLDER;
  }

   public static class  InnereClass{
       private static final Holder HOLDER=new Holder();
  }
}

单例不安全,反射

枚举

package com.vogt.single;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
* @author VOGT
* @version V1.0
* @Package com.vogt.single
* @date 2020/4/13 11:00
*/
//枚举本身也是一个class类
public enum EnumSingle {
  INSTANCE;

  public EnumSingle getInstance(){
    return INSTANCE;
  }
}
class Test{
  public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
      EnumSingle instance1 = EnumSingle.INSTANCE;


      Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class);
      declaredConstructor.setAccessible(true);
      EnumSingle instance2 = declaredConstructor.newInstance();
      //Exception in thread "main" java.lang.NoSuchMethodException: com.vogt.single.EnumSingle.<init>()
      System.out.println(instance1);
      System.out.println(instance2);


  }
}

枚举的(以为)反编译源码:

image-20200413112857690

Compiled from "EnumSingle.java"
public final class com.vogt.single.EnumSingle extends java.lang.Enum<com.vogt.single.EnumSingle> {
public static final com.vogt.single.EnumSingle INSTANCE;
private static final com.vogt.single.EnumSingle[] $VALUES;
public static com.vogt.single.EnumSingle[] values();
public static com.vogt.single.EnumSingle valueOf(java.lang.String);
private com.vogt.single.EnumSingle();
public com.vogt.single.EnumSingle getInstance();
static {};
}

枚举的最终反编译源码:


package com.vogt.single;


public final class EnumSingle extends Enum
{

  public static EnumSingle[] values()
  {
      return (EnumSingle[])$VALUES.clone();
  }

  public static EnumSingle valueOf(String name)
  {
      return (EnumSingle)Enum.valueOf(com/vogt/single/EnumSingle, name);
  }

  private EnumSingle(String s, int i)
  {
      super(s, i);
  }

  public EnumSingle getInstance()
  {
      return INSTANCE;
  }

  public static final EnumSingle INSTANCE;
  private static final EnumSingle $VALUES[];

  static
  {
      INSTANCE = new EnumSingle("INSTANCE", 0);
      $VALUES = (new EnumSingle[] {
          INSTANCE
      });
  }
}

 

posted @ 2020-04-13 11:43  cidevogt  阅读(313)  评论(0)    收藏  举报