Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Solution

The Archiving and Unarchiving pattern relies on objects to encode their own internal state into an archive and later decode themselves from an archive. By implementing their own encoding and decoding, the objects safeguard encapsulation and data hiding. Objects that reference other objects typically give the referenced objects an opportunity to encode themselves as well. Therefore, the act of encoding one object may add many objects to an archive. For example, if an object being encoded has instance variables that are objects, the instance variables are usually also encoded. If an object doesn’t encode its instance variables, it might be incomplete when later unarchived. On the other hand, if an instance variable has a default value or can be calculated from other instance variables, it often makes sense not to encode it. Each object must make its own determination regarding which instance variables to encode.

The key to implementing the Archiving and Unarchiving pattern is the treatment of interrelated objects. No matter how complex the relationships between objects, each object in an archive is only encoded once in that archive. This minimizes the storage required for an archive, but more importantly, it simplifies the restoration of relationships when unarchiving. If many archived objects reference the same object, the unarchived copies will also all reference a single copy of that same object. Similarly, within any group of objects that are being archived, two or more objects might reference each other. Such circular references are resolved automatically in part because of the rule that each object is only represented once in an archive.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint