The last pattern in this chapter, memento, helps us save and restore a state of a complex object. It was originally introduced by the Gang of Four.
When you want to store and restore a current state of a complex object, you can easily run into problems with encapsulation. There may, for example, exist an internal state that is important for the correct functioning of the object, but is not accessible to the public. In such a case, we may not be able to access this state from the code that is not part of the object.
Even if all internal fields are accessible by the public, accessing internal state from external code is a bad practice. The internal representation of an object may change unexpectedly (for example, with a software update), and if a maintainer of such external code is not aware of that, the program would break.
The memento pattern prescribes how a complex object...