160 likes | 359 Views
Memento. Alex “J Lo” Brian C Kiefer. Definitions. A Memento is a way to create an object that previously existed a tiny repository that saves an object’s state. Challenge 19.1. What a few reasons that you might choose to saving a memento in a file rather than as an object?. Challenge 19.1.
E N D
Memento Alex “J Lo” Brian C Kiefer
Definitions • A Memento is • a way to create an object that previously existed • a tiny repository that saves an object’s state
Challenge 19.1 • What a few reasons that you might choose to saving a memento in a file rather than as an object?
Challenge 19.1 • What a few reasons that you might choose to saving a memento in a file rather than as an object? • The ability to restore an object’s state has to survive a system crash. • You anticipate that the user will exit the system and will want to resume work later. • You need to reconstruct an object on another computer.
Applications of Memento • Undo • Your favorite feature • Persistent Storage • String output
Challenge • What is the best data structure to use for Undo?
Challenge • What is the best data structure to use for Undo? • Stack
Challenge • What is the best data structure to use for Undo? • Stack • How can Java assist you in using Memento for persistent storage?
Challenge • What is the best data structure to use for Undo? • Stack • How can Java assist you in using Memento for persistent storage? • Serialization • XML parsing
Challenge 19.5 • What are the disadvantages of storing an object as text?
Challenge 19.5 • What are the disadvantages of storing an object as text? • 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 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.
Challenge 19.5b • What are the advantages of storing an object as text?
Challenge 19.5b • What are the advantages of storing an object 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 setup interchange with RMI, CORBA, or other protocols.
Summary • Used for undo and redo • Forward and Back buttons • Can be used for saving files • Via serialization • Via strings
Can’t remember something? Questions? Comments?
Exercise Console browser, without browsing… • Implement a back command • Implement a forward command • Implement saving session to disk See demo program and problem explanation at http://www.alexlo.net/patterns/