-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemento.java
More file actions
66 lines (53 loc) · 1.09 KB
/
Memento.java
File metadata and controls
66 lines (53 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Memento {
String state;
String time;
public Memento(String state, String time) {
this.state = state;
this.time = time;
}
public String getState() {
return state;
}
public String getTime() {
return time;
}
}
class Originator {
String state;
String name;
public Originator(String state, String name) {
this.state = state;
this.name = name;
}
public Memento saveStateToMemento() {
return new Memento(state, null);
}
public void getStateFromMemento(Memento m) {
state = m.getState();
}
public void setState(String state) {
this.state = state;
}
}
class CareTaker {
List<Memento> mementoList;
public CareTaker() {
mementoList = new ArrayList<Memento>();
}
public void addMemento(Memento m) {
mementoList.add(m);
}
public Memento restore(int i) {
return mementoList.get(i);
}
}
class Test {
public static void main(String[] args) {
Originator o = new Originator("true", "Tom");
CareTaker ct = new CareTaker();
ct.add(o.saveStateToMemento());
o.setState("False");
ct.add(o.saveStateToMemento());
o.getStateFromMemento(ct.restore(0));
}
}