We know that a singleton class can normally produce only one instance, in order to be able to convert a singleton class to multiple classes (this is not necessary to achieve this purely for implementation).
We can make the single instance class implement the Serializable interface and serialize
Then deserialization, in the case of deserialization, because the single instance class implements the serialization interface, it automatically creates a new class that reaches multiple effects
If you still want to generate a single instance class to add the Readresolve () method to a single instance class if the single instance class implements the serialization interface
Package com.java.single;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import java.io.ObjectStreamException;
Import java.io.Serializable; /** * We know that a singleton class would normally produce only one instance, in order to be able to convert a single instance class to multiple instances (this is not necessary here for implementation only) * We can have the single instance class implement the Serializable interface, serialize * and then deserialize, and in the reverse sequence,
Because the single instance class implements the serialization interface, as a result, a new class is automatically created to reach multiple effects * If you still want to create a single instance class with the serialization interface implemented in a single instance class you can add the Readresolve () method to a single instance class ()/public class Data {///serialization
public static void WriteObject () throws exception{worm worm = worm.getinstance ();
ObjectOutputStream out = new ObjectOutputStream (New FileOutputStream ("D://worm.out"));
Out.writeobject (worm); Out.close (),//close also flushes empty buffers}//deserialization public static Worm ReadObject () throws exception{objectinputstream in = new O
Bjectinputstream (New FileInputStream ("D://worm.out"));
Worm Readworm = (worm) in.readobject ();
return readworm;
public static void Main (string[] args) throws exception{//WriteObject (); Worm Worm1 = ReadObject ();
Worm Worm2 = ReadObject ();
System.out.println (Worm1 = = Worm2);
Class Worm implements serializable{private static final long serialversionuid = 1L; private static Worm instance = new Worm ()//Private instance member, the Singleton object Private Worm () {//Private construction method is created when the class loads, avoiding external creation of the instance} public stat IC Worm getinstance () {//Only this has a public static method, gets an instance return instance;//returns the instance}/** * If the Singleton class implements the serialization interface, the new class is automatically created when the deserialization is done, *
To solve this problem, you can use the Readresolve () method//Private Object Readresolve () throws Objectstreamexception {//return instance; // }
}
Why deserialization produces a new object
So worm Readworm = (worm) in.readobject (); deserialization, just simple recovery data to create the object, and not to invoke any constructor,
There is no way to tell if an instance of the object has already existed, so the deserialization is a new object