The memento pattern requires the implementation of three distinct entities:
- Memento: a representation of the internal state of Originator, which should be immutable
- Originator: the original object that can produce and consume the Memento, in order to save and restore its own state
- CareTaker: an external object that stores and restores a Memento to an Originator
Memento, in other languages, may be implemented as an opaque box so the state can't be mutated. In Swift, we can leverage structs, their immutability, and the fact they are passed by value and not reference. It is critical that different Memento objects do not share memory.
Let's start by implementing a generic memento pattern. Thanks to Swift and ...