Memento
Intent
- Without violating encapsulation, capture and externalize an object's
internal state so that the object can be returned to this state later.
[GoF, p283]
- A magic cookie that encapsulates a "check point" capability.
- Promote undo or rollback to full object status.
Problem
Need to restore an object back to its previous state (e.g. "undo"
or "rollback" operations).
Structure Summary
Structure category: promote X to "full object status"
Similar patterns:
Prototype
Command
Iterator
Mediator
Discussion
The client requests a Memento from the source object when it needs to
checkpoint the source object's state. The source object initializes
the Memento with a characterization of its state. The client is the
"care-taker" of the Memento, but only the source object can store and
retrieve information from the Memento (the Memento is "opaque" to the
client and all other objects). If the client subsequently needs to
"rollback" the source object's state, it hands the Memento back to the
source object for reinstatement.
An unlimited "undo" and "redo" capability can be readily implemented
with a stack of Command objects and a stack of Memento objects.
The Memento design pattern defines three distinct roles:
- Originator - the object that knows how to save itself.
- Caretaker - the object that knows why and when the Originator needs
to save and restore itself.
- Memento - the lock box that is written and read by the Originator,
and shepherded by the Caretaker.
Structure
The Engine knows how to save and restore itself. The Client knows when.
The Engine stores its state in a Memento "lock box", and puts it
on deposit with the Client. The Client subsequently decides if,
and when, the Memento is returned to the Engine.
Example
The Memento captures and externalizes an object's internal state so
that the object can later be restored to that state. This pattern is
common among do-it-yourself mechanics repairing drum brakes on their
cars. The drums are removed from both sides, exposing both the right
and left brakes. Only one side is disassembled and the other serves as
a Memento of how the brake parts fit together. Only after the job has
been completed on one side is the other side disassembled. When the
second side is disassembled, the first side acts as the Memento.
[Michael Duell, "Non-software examples of software design
patterns", Object Magazine, Jul 97, p54]
Check list
- Identify the roles of “caretaker” and “originator”.
- Create a Memento class and declare the originator a friend.
- Caretaker knows when to "check point" the originator.
- Originator creates a Memento and copies its state to that Memento.
- Caretaker holds on to (but cannot peek into) the Memento.
- Caretaker knows when to "roll back" the originator.
- Originator reinstates itself using the saved state in the Memento.
Before and after
Before | | After |
tbd
| |
// "check point" and "roll back" a Stack
class Memento {
friend class Stack;
int *m_items, m_num;
Memento( int* arr, int num ) {
m_items = new int[m_num = num];
for (int i=0; i < m_num; i++)
m_items[i] = arr[i];
}
public:
~Memento() { delete m_items; }
};
class Stack {
int m_items[10], m_sp;
public:
Stack() { m_sp = -1; }
void push( int in ) { m_items[++m_sp] = in; }
int pop() { return m_items[m_sp--]; }
bool is_empty() { return (m_sp == -1); }
Memento* check_point() {
return new Memento( m_items, m_sp+1 );
}
void roll_back( Memento* mem ) {
m_sp = mem->m_num-1;
for (int i=0; i < mem->m_num; ++i)
m_items[i] = mem->m_items[i];
}
friend ostream& operator<< ( ostream& os, const Stack& s ) {
string buf( "[ " );
for (int i=0; i < s.m_sp+1; i++) {
buf += s.m_items[i]+48;
buf += ' ';
}
buf += ']';
return os << buf;
}
};
int main( void ) {
Stack s;
for (int i=0; i < 5; i++)
s.push( i );
cout << "stack is " << s << '\n';
Memento* first = s.check_point();
for (int i=5; i < 10; i++)
s.push( i );
cout << "stack is " << s << '\n';
Memento* second = s.check_point();
cout << "popping stack: ";
while ( ! s.is_empty())
cout << s.pop() << ' ';
cout << '\n';
cout << "stack is " << s << '\n';
s.roll_back( second );
cout << "second is " << s << '\n';
s.roll_back( first );
cout << "first is " << s << '\n';
cout << "popping stack: ";
while ( ! s.is_empty())
cout << s.pop() << ' ';
cout << '\n';
delete first; delete second;
}
// stack is [ 0 1 2 3 4 ]
// stack is [ 0 1 2 3 4 5 6 7 8 9 ]
// popping stack: 9 8 7 6 5 4 3 2 1 0
// stack is [ ]
// second is [ 0 1 2 3 4 5 6 7 8 9 ]
// first is [ 0 1 2 3 4 ]
// popping stack: 4 3 2 1 0
|
Rules of thumb
Command and Memento act as magic tokens to be passed around and invoked
at a later time. In Command, the token represents a request; in
Memento, it represents the internal state of an object at a particular
time. Polymorphism is important to Command, but not to Memento because
its interface is so narrow that a memento can only be passed as a
value. [GoF, p346]
Command can use Memento to maintain the state required for an undo
operation. [GoF, p242]
Memento is often used in conjunction with Iterator. An Iterator can use
a Memento to capture the state of an iteration. The Iterator stores the
Memento internally. [GoF, p271]