Using the memento pattern
Every time you need to store the existing state of an object in order to be able to restore it later, you can probably leverage the memento pattern. The main advantage of this pattern is the opacity of the state. The CareTaker isn't really dependent on the Memento type, as we've seen.
The protocols we showed can serve as a basis for your custom implementations. Following this pattern lets you implement multiple CareTaker objects with different features.
Just to note, in our example, the restoration process means that any redo information is lost. It would be possible to keep this information until the next save call by not popping last, but simply moving the current restoration point. Once again, it depends on your ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access