I am trying to write a Serializable Singleton class by adding the readResolve () method. My intention is to get the same object with its state of the object during serialization.
below is my sample code example:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class SingletonDemo { public static void main(String[] args) { Singleton obj = Singleton.getInstance(); System.out.println("After NEW Object creation : " + obj); obj.i = 5; System.out.println("Object modified"); System.out.println("After Object 1st Modification : " + obj); serializeMe(); System.out.println("Serialized successfully with object state : " + obj); obj.i = 10; System.out.println("Object modified again"); System.out.println("After Object 2nd Modification : " + obj); Singleton st = (Singleton)deSerializeMe(); System.out.println("Deserialized successfully"); System.out.println("After Deserialization : " + st); } public static void serializeMe() { FileOutputStream fos; ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("d:\\SingletonData.txt")); oos.writeObject(Singleton.getInstance()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static Object deSerializeMe() { ObjectInputStream oin = null; Object obj = null; try { oin = new ObjectInputStream(new FileInputStream("d:\\SingletonData.txt")); obj = oin.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return obj; } } class Singleton implements Serializable { int i; private static Singleton obj = null; private Singleton() { System.out.println("Executing constructor"); i=1; } public static Singleton getInstance() { if(obj == null) { obj = new Singleton(); } System.out.println("An instance is returned"); return obj; } public Object readResolve() { System.out.println("Executing readResolve"); return Singleton.getInstance();
OUTPUT:
Executing constructor An instance is returned After NEW Object creation : Singleton [i=1] Object modified After Object 1st Modification : Singleton [i=5] An instance is returned Serialized successfully with object state : Singleton [i=5] Object modified again After Object 2nd Modification : Singleton [i=10] Executing readResolve An instance is returned Deserialized successfully After Deserialization : Singleton [i=10]
I know that the current script will always return the same Singleton instance with the last state of the object.
I tried to override writeObject () and readObject () (commented on this code above), but did not get the desired result. i.e.
After Deserialization : Singleton [i=5]
But in readResolve () there is no reference to ObjectInputStream, so I can get the instance and update it using the state of the serialized object before returning.
Please correct me if I am mistaken in my concept and help me solve this problem.
Thanks.
source share