Design Pattern Download: The Memento Pattern
1 min readApr 15, 2022
The Memento pattern is a Behavioral Pattern used in software engineering when we want to save and restore the state of an object.
Example
Imagine we have a video game that we are playing, the user occasionally wants to save his or her progress and load the progress up at a later time, what do you do? Do you create an array of these game objects which potentially have a lot more data and overhead than what you are actually trying to save? NO! The solution is to create a lightweight object that just stores the state!
When to use a Memento?
- When you want to produce snapshots of an object’s current state.
What is the Memento composed of?
- The Memento (the state saving object)
- The Concrete Object (the thing who’s state we are trying to take a snapshot of)
- The CareTaker (the thing that manages the list of saves and restores th game to the desired state)
Implementation
The Memento
The Concrete Object
The CareTaker
Now we can save and restore our game’s state as we please!
final state
first state
second state