设计模式之使用序列化和反序列化实现单例模式

什么是序列化和反序列化

序列化 :把Java对象转换为字节码
反序列化:把字节码还原为Java对象

示例代码
import java.io.*;
class Users implements Serializable{
    private String username;
    private String password;
    public Users(String username,String password){
        this.username=username;
        this.password=password;
    }
    public String getUsername(){
        return this.username;
    }
    public String getPassword(){
        return this.password;
    }
    public String toString(){
        return "name="+this.username+"   "+"password="+this.password;
    }
}
public class ObjectWriteandRead{
    public static String path="F:\\NotePadPP\\";
    public static String ObjectFilename="users.txt";
    public static void main(String[]args){
        Users user=new Users("Tom","123");
        try{
            File file=new File(path+ObjectFilename);
            file.createNewFile();
            FileOutputStream fileoutputstream = new FileOutputStream(file);  
            ObjectOutputStream objectoutputstream = new ObjectOutputStream(fileoutputstream);  
            objectoutputstream.writeObject(user);  
            objectoutputstream.flush();  
            objectoutputstream.close();  
            fileoutputstream.close();  

            FileInputStream fileinputstream = new FileInputStream(file);  
            ObjectInputStream objectinputstream = new ObjectInputStream(fileinputstream);  
            Users userinput = (Users) objectinputstream.readObject();  
            objectinputstream.close();  
            fileinputstream.close();  
            System.out.println(userinput.toString());
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }

    }
}

输出结果:
这里写图片描述

使用序列化和反序列化实现单例模式

import java.io.*;
class MyOject implements Serializable{
    private static class MyOjectHandler{
        private static MyOject myObject=new MyOject();
    }
    private MyOject(){}
    public static MyOject getInstance(){
        return MyOjectHandler.myObject;
    }
    protected Object readResolve()throws ObjectStreamException{
        return MyOjectHandler.myObject;
    }
}
public class ObjectWriteandRead{
    public static String path="F:\\NotePadPP\\";
    public static String ObjectFilename="object.txt";
    public static void main(String[]args){
        MyOject myObject=MyOject.getInstance();
        try{
            File file=new File(path+ObjectFilename);
            file.createNewFile();
            FileOutputStream fileoutputstream = new FileOutputStream(file);  
            ObjectOutputStream objectoutputstream = new ObjectOutputStream(fileoutputstream);  
            objectoutputstream.writeObject(myObject);  
            objectoutputstream.flush();  
            objectoutputstream.close();  
            fileoutputstream.close();  
            System.out.println(myObject.hashCode());

            FileInputStream fileinputstream = new FileInputStream(file);  
            ObjectInputStream objectinputstream = new ObjectInputStream(fileinputstream);  
            MyOject myObjectinput = (MyOject) objectinputstream.readObject();  
            objectinputstream.close();  
            fileinputstream.close();  
            System.out.println(myObjectinput.hashCode());
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }

    }
}

输出结果:
这里写图片描述

关键代码:

protected Object readResolve()throws ObjectStreamException{
        return MyOjectHandler.myObject;
    }

如果不在MyObject中调用写这段代码就不会得到单例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值