Using Strings as Mementos

Object serialization provides a simple means for a memento to persist across multiple sessions that occur, say, on a given day. If, on the other hand, your user wants to revive a simulation after a six-month sabbatical, he or she may find that various classes in the simulation package have evolved to new versions. The java.io package provides some support for deserializing an object when its class has changed since the serialization was written, but proper handling of object serialization across class versions is, in practice, problematic. To avoid this problem, you can “serialize” your object to text.

One way to provide persistent storage of an object is to write out a textual description of it. You can save an object's ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.