单例模式:保证一个类只有一个实例,并且提供一个访问该实例的全局访问点
单例设计模式上初篇

单例设计模式上初篇
单例设计模式上初篇
单例设计模式上初篇
单例设计模式上初篇
单例设计模式上初篇
单例设计模式上初篇

单例设计模式上初篇
单例设计模式上初篇

  • 反射与反序列化的使用
package learn.java.com.java.pattern;

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

public class SingletonDemo01 {
   public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
       SingletonDemo s1=SingletonDemo.getInstance();
       SingletonDemo s2=SingletonDemo.getInstance();
       System.out.println(s1);
       System.out.println(s2);
       //通过反射的方式直接调用
       Class<SingletonDemo> clazz =(Class<SingletonDemo>) Class.forName("learn.java.com.java.pattern.SingletonDemo");
       Constructor<SingletonDemo> c=clazz.getDeclaredConstructor(null);
       c.setAccessible(true);
//       (可以访问私有变量)
       SingletonDemo  s3=c.newInstance();
       SingletonDemo s4=c.newInstance();
       System.out.println(s3);
       System.out.println(s4);
       /**没有防止反射时的返回
        * [email protected]
        * [email protected]
        * [email protected]
        * [email protected]
        */

        //序例化
       FileOutputStream fos =new FileOutputStream("./a.txt");
       ObjectOutputStream oos =new ObjectOutputStream(fos);
       oos.writeObject(s1);
       oos.close();
       oos.close();
       ObjectInputStream ois=new ObjectInputStream(new FileInputStream("./a.txt"));
       SingletonDemo snew=(SingletonDemo) ois.readObject();
       System.out.println(snew);

//没有添加readResolve 时输出对象
       /**
        *[email protected]
        * [email protected]
        * [email protected]
        */


//添加readResolve 时输出对象
       /**
        [email protected]
        [email protected]
        [email protected]
        */

   }
}
 class SingletonDemo implements Serializable {
    private static SingletonDemo instance=new SingletonDemo();
    //加载类时,天然是线程安全的
    //类初始化时立即加载(不延时)
//    private SingletonDemo(){}
    private SingletonDemo(){
        if (instance!=null){
           throw new RuntimeException();
        }//用来防止反射
    }
    //方法没有同步,调用效率高
    public static SingletonDemo getInstance(){
        return instance;
    }
//用来防止反序列化,反序列化时如果定义了readsolve()则直接返回指定的对象,而不重建对象
    public Object readResolve() {
        return instance;
    }
}
 class SingletonDemo02{
    private static SingletonDemo02 s;
    private SingletonDemo02(){}
    private static synchronized SingletonDemo02 getInstance(){
        if(s==null)
            s=new SingletonDemo02();
        return s;
    }

}

class SingletonDemo0{
    private static class SingletonDemo04{
        private static SingletonDemo04 s=new SingletonDemo04();
        private static SingletonDemo04 getInstance(){
            return s;
        }
    }
}


 enum Sing{
    instance;
}

相关文章:

  • 2021-09-02
  • 2021-09-13
  • 2021-12-15
  • 2021-08-01
  • 2021-12-10
  • 2022-12-23
猜你喜欢
  • 2021-05-16
  • 2022-12-23
  • 2021-12-01
  • 2021-07-03
  • 2021-08-27
  • 2021-12-01
  • 2022-12-23
相关资源
相似解决方案