Serialization-Understanding readresolve ()

Source: Internet
Author: User
Tags object serialization

From: http://blog.csdn.net/haydenwang8287/archive/2010/10/25/5964130.aspx

Serialization is a very common and powerful function in Java. In my opinion, it is one of the most common functions of Java to save Java objects to a disk and read them from the disk later. In basic cases, serialization can "simply work )". However, as more and more complex object formats and design patterns are adopted, the possibility of transparent object serialization being "simple to work" becomes increasingly impossible. Processing an instance of a controllable set, such as singleton and enum, is a scenario where serialization requires some extra help.

In any scenario where the single-instance object can be sequenced, it is very important to ensure that the single-instance object is used. This is implemented through the readresolve () interface. Singleton is a good example:

 

  1. Public final class mysingleton {
  2. Private mysingleton (){}
  3. Private Static final mysingleton instance = new mysingleton ();
  4. Public static mysingleton getinstance () {return instance ;}
  5. }

 

 

In the above example, there is only one way to get the instance of mysingleton-that is, use the getinstance () method. Then, after simply adding an interface implementation, this code becomes unavailable:

 

  1. Public final class mysingleton implements serializable {
  2. //.

 

 

Now, with the serializable tool, we can write a single instance object to a disk and then read it back to effectively obtain an instance. Even if the constructor is private, the serializable tool can still create a new instance of the class in a special way. The serialization operation provides a very special hook-class with a private instantiated method readresolve (), this method ensures that class developers have a say in what object will be returned by serialization. It is strange enough that readresolve () is not static, but is referenced when serialization creates an instance. We started to experience this in one minute. The following example shows how readresolve () plays a role in our singleton:

 

  1. Public final class mysingleton {
  2. Private mysingleton (){}
  3. Private Static final mysingleton instance = new mysingleton ();
  4. Public static mysingleton getinstance () {return instance ;}
  5. Private object readresolve () throws objectstreamexception {
  6. // Instead of the object we're on,
  7. // Return the class variable instance
  8. Return instance;
  9. }
  10. }

 

 

So far, the situation is quite good. However, when processing multiple instances, the situation becomes a bit complicated. To explain this, I will demonstrate this through a type-safe enmumeration. Remember that the enum type of JDK 5 has automatically handled this readresolve situation. The following is an example of a small enumeration:

 

  1. Public final class sides {
  2. Private int value;
  3. Private sides (INT newval) {value = newval ;}
  4. Private Static final int left_value = 1;
  5. Private Static final int right_value = 2;
  6. Private Static final int top_value = 3;
  7. Private Static final int bottom_value = 4;
  8. Public static final left = new sides (left_value );
  9. Public static final right = new sides (right_value );
  10. Public static final Top = new sides (top_value );
  11. Public static final bottom = new sides (bottom_value );
  12. }

 

 

Now we can implement serialization to determine whether the key of the returned instance depends on the value set by the object itself:

 

  1. Public final class sides implements serializable {
  2. Private int value;
  3. Private sides (INT newval) {value = newval ;}
  4. Private Static final int left_value = 1;
  5. Private Static final int right_value = 2;
  6. Private Static final int top_value = 3;
  7. Private Static final int bottom_value = 4;
  8. Public static final left = new sides (left_value );
  9. Public static final right = new sides (right_value );
  10. Public static final Top = new sides (top_value );
  11. Public static final bottom = new sides (bottom_value );
  12. Private object readresolve () throws objectstreamexception {
  13. // Switch on this instance's value to figure out which class variable
  14. // This is meant to match
  15. Switch (value ){
  16. Case left_value: return left;
  17. Case right_value: return right;
  18. Case top_value: Return top;
  19. Case bottom_value: Return bottom;
  20. }
  21. Return NULL;
  22. }
  23. }

 

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.