The core intent is to capture an object's internal state (for later restoration) without violating encapsulation (i.e. uncovering internals for the purpose of externalization). Therefore, the state-representing object - called the memento - should satisfy 2 criteria:
- The memento should be opaque (aka provide no insight)
- The only allowed action with a memento is to return it to its originator for the purpose of state restoration. (This one may be relaxed in some variants to allow passing of states between sibling instances.)
Memento is extremely useful with transactional processing semantics, i.e. when an action is expected to either entirely succeed and change the system state in a defined way, or, on failure, not to change the system state at all.
A MementoClosure is a generic python implementation of the memento pattern. An example how to use the memento closure follows: