Design Patterns Java™ Workbook

(Michael S) #1
Appendix B. Solutions

method should not have to check that the mementos stack has at least one memento. But it
doesn't hurt.


SOLUTION 19.4....................................................................................................................................


The latest memento contains the state to save, so the object to write to disk is
mementos.peek().


SOLUTION 19.5....................................................................................................................................


The disadvantages of storing an object in textual format follow.



  • Encapsulation flies out the window. Anyone with a text editor can manipulate the
    object's data.

  • For this approach to work, you must be able to parse the text, either using an XML
    parser or writing your own parser for a proprietary format.

  • A textual representation may be much larger than an object serialization with the same
    information.


On the other hand, there are many advantages to storing objects as text.



  • Anyone with a text editor can verify an object's data.

  • Anyone with a text editor can manipulate an object's data—an advantage in some
    contexts.

  • It is often easier to pass text between systems than it is to set up interchange with
    RMI, CORBA, or other protocols.

Free download pdf