Memento pattern variations
In some cases, the plain Memento pattern is enough to get things rolling, but there will always be situations where more features are needed to make your project the best version of itself. For instance, storing a single Memento
object is usable but not ideal for scenarios where you want to have a history of data snapshots that can be undone and redone at your convenience, or injecting an Originator
instance into your Caretaker
class constructor (yes, it’s more coupling, but it also puts all the memento management responsibilities into a single place).
Again, these are optional, but I’ve found them extremely useful in my own projects (hopefully they spark your creativity when thinking of different ways to apply the Memento pattern in your own code).
Storing memento history
The Memento pattern is one of the best go-to solutions for creating a robust undo/redo system, so let’s write a basic implementation to see how it works...