单例模式与反序列化

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * 饿汉式
 */
public class TestSingleE implements Serializable {
    private TestSingleE(){}
    public final static TestSingleE testSingle=new TestSingleE();
    private Object readResolve() throws ObjectStreamException {
        return  testSingle;
    }
}
import java.io.ObjectStreamException;
import java.io.Serializable;
/**
 * 懒汉式
 */
public class TestSingleL implements Serializable {
    private TestSingleL(){
        if(instance!=null){
            throw new RuntimeException();
        }
    }
    private static TestSingleL instance=null;
    public synchronized static TestSingleL getInstance(){
        if(instance==null){
            instance=new TestSingleL();
        }
        return instance;
    }

    //反序列化时,如果定义了readResolve()则直接返回此方法指定的对象,而不需要单独再创建新对象。
    private Object readResolve() throws ObjectStreamException {
        return instance;
    }
}

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * 双重检测锁式
 */
public class TestSingleS implements Serializable {
    private static  TestSingleS instance=null;
    private TestSingleS(){}
    public static TestSingleS getInstance(){
        if(instance==null){
            TestSingleS ts;
            synchronized (TestSingleS.class){
                ts=instance;
                if (ts==null){
                    synchronized (TestSingleS.class){
                        ts=new TestSingleS();
                    }
                    instance=ts;
                }
            }
        }
        return instance;
    }
    //反序列化时,如果定义了readResolve()则直接返回此方法指定的对象,而不需要单独再创建新对象。
    private Object readResolve() throws ObjectStreamException {
        return TestSingleS.instance;
    }
}
import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * 静态内部类式
 */
public class TestSingleJ implements Serializable {
     private static class StateTestSingleJ{
        public static final TestSingleJ instance= new TestSingleJ();
    }
    private TestSingleJ(){}
    public static TestSingleJ getInstance(){
        return  StateTestSingleJ.instance;
    }

    /**
     * 反序列化时,如果定义了readResolve()则直接返回此方法指定的对象,而不需要单独再创建新对象。
     * @return
     * @throws ObjectStreamException
     */
    private Object readResolve() throws ObjectStreamException {
        return StateTestSingleJ.instance;
    }
}
import java.io.Serializable;

/**
 * 枚举式
 */
public enum TestSingleM implements Serializable {
    /**
     *   这个枚举元素,本身就是单列对象
     */
    INSTANCE;

    /**
     *   添加自己需要的操作
     */
    public void singletonOperation(){
        System.out.println("枚举式");
    }
}
import java.io.Serializable;

/**
 * 枚举式
 */
public enum TestSingleM implements Serializable {
    /**
     *   这个枚举元素,本身就是单列对象
     */
    INSTANCE;

    /**
     *   添加自己需要的操作
     */
    public void singletonOperation(){
        System.out.println("枚举式");
    }
}
import java.io.*;

public class TestSingleGof {
    public static void main(String[] args) throws IOException {
        TestSingleE tse1= TestSingleE.testSingle;
        TestSingleE tse2= TestSingleE.testSingle;
        operationIo(tse1,"饿汉式");
        System.out.println(tse1.hashCode()+"=="+tse2.hashCode()+"=====>"+(tse1.hashCode()==tse2.hashCode()));

        TestSingleL tsl1=TestSingleL.getInstance();
        TestSingleL tsl2=TestSingleL.getInstance();
        operationIo(tsl1,"懒汉式");
        System.out.println("经过定义readResolve()方法进行防反序列化处理");
        System.out.println(tsl1.hashCode()+"=="+tsl2.hashCode()+"=====>"+(tsl1.hashCode()==tsl2.hashCode()));

        TestSingleS tss1=TestSingleS.getInstance();
        TestSingleS tss2=TestSingleS.getInstance();
        operationIo(tss1,"双重检测锁式");
        System.out.println(tss1.hashCode()+"=="+tss2.hashCode()+"=====>"+(tss1.hashCode()==tss2.hashCode()));

        TestSingleJ tsj1=TestSingleJ.getInstance();
        TestSingleJ tsj2=TestSingleJ.getInstance();
        operationIo(tsj1,"静态内部类式");
        System.out.println(tsj1.hashCode()+"=="+tsj2.hashCode()+"=====>"+(tsj1.hashCode()==tsj2.hashCode()));

        TestSingleM tsm1=TestSingleM.INSTANCE;
        TestSingleM tsm2=TestSingleM.INSTANCE;
//        tsm1.singletonOperation();
        operationIo(tsm1,"枚举式");
        System.out.println(tsm1.hashCode()+"=="+tsm2.hashCode()+"=====>"+(tsm1.hashCode()==tsm2.hashCode()));

        operation("11","233","333","3445545");
    }

    static void operation(String ...a){
        System.out.println(a[1]);
    }

    static void operationIo(Object t1,String msg) throws IOException {
        System.out.println("------------------------"+msg+"-------------------------");
        File file = new File("/Users/jq/IdeaProjects/Demo/TestSingleL.txt");
        if (file.exists()){
            System.out.println("文件已存在");
        }else if(file.createNewFile()){
            System.out.println("文件创建成功:"+file.getPath());
        }
        try {
            //Users对象序列化过程
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            //调用 ObjectOutputStream 中的 writeObject() 方法 写对象
            oos.writeObject(t1);
            oos.flush();        //flush方法刷新缓冲区,写字符时会用,因为字符会先进入缓冲区,将内存中的数据立刻写出
            fos.close();
            oos.close();

            //Users对象反序列化过程
            FileInputStream fis = new FileInputStream(file);
            //创建对象输入流
            ObjectInputStream ois = new ObjectInputStream(fis);
            //读取对象
            Object t2 = ois.readObject();           //会抛出异常(类找不到异常)
            System.out.println("反序列化之前:"+t1.hashCode()+"\n"+"反序列化以后:"+t2.hashCode());
            System.out.println("是否为同一对象:"+(t1.hashCode()==t2.hashCode()));
            ois.close();
            fis.close();

        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值