Memento Pattern
Definition
To record an object internal state without violating encapsulation and reclaim it later without knowledge of the original object. A memento is an object that stores a snapshot of the internal state of another object.Where to use
•When letting some info in an object be available by another object.•When you want to create snapshots of a state for an object.
•When you need undo/redo features.
Benefits
Ability to restore an object to its previous state.Drawbacks/consequences
Care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object. Using memento to store large amounts of data from Originator might be expensive if clients create and return mementos frequently.Memento Pattern Class Diagram
•An instance from the Originator class is the object that knows how to save itself. Uses memento to restore itself.
•An instance from the Caretaker class is the object that knows why and when the Originator needs to save and restore itself. Never operates or examines the contents of memento
•An instance of the Memento class is the lock box that is written and read by the Originator, and shepherded by the Caretaker.
The originator is any object that has an internal state, that we want to take a snapshot of. The caretaker is going to do something to the originators state, but wants to be able to later restore the originators state. The caretaker first asks the originator for a memento object, containing the snapshot. Then it performs the sequence of operations it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.
Memento Pattern Example
01.
public
class
Originator {
02.
private
String state;
03.
/* lots of memory using private data that does not have to be
04.
saved. Instead we use a small memento object. */
05.
public
void
set(String state) {
06.
System.out.println(
"Originator: Setting state to "
+ state);
07.
this
.state = state;
08.
}
09.
public
Object saveToMemento() {
10.
System.out.println(
"Originator: Saving to Memento."
);
11.
return
new
Memento(state);
12.
}
13.
public
void
restoreFromMemento(Object m) {
14.
if
(m
instanceof
Memento) {
15.
Memento memento = (Memento) m;
16.
state = memento.getSavedState();
17.
System.out.println("Originator: State after restoring
18.
from Memento: " + state);
19.
}
20.
}
21.
}
22.
import
java.util.ArrayList;
23.
import
java.util.List;
24.
public
class
Caretaker {
25.
private
List<Object> savedStates =
new
ArrayListList<Object>();
26.
public
void
addMemento(Object m) {
27.
savedStates.add(m);
28.
}
29.
public
Object getMemento(
int
index) {
30.
return
savedStates.get(index);
31.
}
32.
}
33.
public
class
Memento {
34.
private
String state;
35.
public
Memento(String stateToSave) {
36.
state = stateToSave;
37.
}
38.
public
String getSavedState() {
39.
return
state;
40.
}
41.
}
42.
43.
public
class
MementoExample {
44.
public
static
void
main(String[] args) {
45.
Caretaker caretaker =
new
Caretaker();
46.
Originator originator =
new
Originator();
47.
originator.set(
"State1"
);
48.
originator.set(
"State2"
);
49.
caretaker.addMemento(originator.saveToMemento());
50.
originator.set(
"State3"
);
51.
caretaker.addMemento(originator.saveToMemento());
52.
originator.set(
"State4"
);
53.
originator.restoreFromMemento(caretaker.getMemento(
1
));
54.
}
55.
}
c:>Originator: Setting state to State1
c:>Originator: Setting state to State2
c:>Originator: Saving to Memento.
c:>Originator: Setting state to State3
c:>Originator: Saving to Memento.
c:>Originator: Setting state to State4
c:>Originator: State after restoring from Memento: State3
0 comments:
Post a Comment