How to make a single column class can become a class of multiple cases

Source: Internet
Author: User
Tags serialization

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.