1501
Comment:
|
1550
occam strikes again
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
## page was renamed from MementoPatternInPython |
The memento pattern is defined on [http://en.wikipedia.org/wiki/Memento_pattern wikipedia], and discussed on Ward's wiki under [MementoPattern].
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 [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413838 MementoClosure] is a generic python implementation of the memento pattern. An example how to use the memento closure follows: