Memento Pattern Yuli Ge, Lukas Herrmann, Sheil Patel Team 19

Memento Pattern
Yuli Ge, Lukas Herrmann, Sheil Patel
Team 19
What is Memento?
●
●
provides the ability to restore an object to its previous state
○ for example, using an undo function via rollback or back stack
implemented with three objects:
○ memento
■ stores internal state of the originator object
■ state can include any number of state variables
○ originator
■ creates a memento object capturing the originators internal state
■ use the memento object to restore its previous state
○ caretaker
■ responsible for keeping the memento
Memento Pattern Model
Code Example - Originator/Memento
Code Example - Caretaker
Code Example - Memento Test
OUTPUT
Why is Memento Useful?
● Useful when you want to add an undo mechanism in
your application
○
Using CTRL-Z to undo typed text, for example
■ System can save states every so often using a Memento
■ CTRL-Z will revert back to previously saved Memento instances
● Using serialization along with this pattern, it's easy to
preserve the object state and bring it back later on
Downsides to Memento
● Saving or restoring of states can be a time consuming
process.
● If used incorrectly, it can expose the internal structure of
your object
○
Essentially allowing any other object to change the state of your
object.
Questions?