Java设计模式之使用反序列化的方式实现单例模式

package com.sinosoft.test;

import java.io.Serializable;

/**
* 使用序列化的方式实现单例模式
*/
public class HungrySingleton implements Serializable {
private static HungrySingleton instance=new HungrySingleton();

public static HungrySingleton getInstance(){
return instance;
}

private HungrySingleton(){

}
}

创建测试类;实现序列化到此判断过程
package com.sinosoft.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
* Created by ZhaoYongqiang on 2020/8/30.
*/
public class SerializableTest {
public static void main(String[] args) throws FileNotFoundException,IOException,ClassNotFoundException {
HungrySingleton instance=HungrySingleton.getInstance();

ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("instance"));
outputStream.writeObject(instance);
outputStream.close();
System.out.println("序列化到磁盘成功");
//
// ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("instance"));
// HungrySingleton object =(HungrySingleton) objectInputStream.readObject();
// System.out.println("反序列化成功");

}
}
运行结果:

 

 

可见是能够序列化成功的。

接下来在进行反序列化,代码在上面:

 接下来我们看一下序列化和反序列化是否是同一个实例:

package com.sinosoft.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
* Created by ZhaoYongqiang on 2020/8/30.
*/
public class SerializableTest {
public static void main(String[] args) throws FileNotFoundException,IOException,ClassNotFoundException {
HungrySingleton instance=HungrySingleton.getInstance();

ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("instance"));
outputStream.writeObject(instance);
outputStream.close();
System.out.println("序列化到磁盘成功");

ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("instance"));
HungrySingleton object =(HungrySingleton) objectInputStream.readObject();
objectInputStream.close();
System.out.println(object);
System.out.println("反序列化成功");

System.out.println("序列化和反序列化是否是同一个实例:"+(instance==object));
}
}

运行结果:

序列化到磁盘成功
com.sinosoft.test.HungrySingleton@12edcd21
反序列化成功
序列化和反序列化是否是同一个实例:false

可见,已经不是同一个实例了。

那么怎样解决这种问题呢?指定签名

package com.sinosoft.test;

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

/**
* 使用序列化的方式实现单例模式
*/
public class HungrySingleton implements Serializable {
private static HungrySingleton instance=new HungrySingleton();

public static HungrySingleton getInstance(){
return instance;
}

private HungrySingleton(){

}

Object readResolve() throws ObjectStreamException{
return instance;
}
}

再次运行:

序列化到磁盘成功
com.sinosoft.test.HungrySingleton@4d7e1886
反序列化成功
序列化和反序列化是否是同一个实例:true

这个序列化ID加不加都是可以的。

private final long serialVersionUID=42L;

posted on 2020-08-30 11:03  ~码铃薯~  阅读(308)  评论(0编辑  收藏  举报

导航