Summary
And that brings us to the close of our Memento exploration. We’ve covered a lot of ground here, but it’s for a good cause. Saving and restoring state snapshots is not only useful but crucial in almost any meaningful application or game. Without it, your users (and players) may feel like they don’t have any agency when it comes to making mistakes or being creative (for fear that they can’t spam the back button if they don’t like where they end up).
Remember, the Memento pattern works best when you need to save and restore internal state information without breaking an object’s encapsulation. The Originator
class is responsible for creating and restoring Memento
snapshots, your Memento
is a storage-only class that mirrors whatever Originator
data you want to save, and the Caretaker
class stores and provides the current Memento
on demand. You’re not limited to a single stored snapshot because the Caretaker
class can be upgraded...