-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS18_Memento.java
More file actions
76 lines (55 loc) · 1.37 KB
/
DS18_Memento.java
File metadata and controls
76 lines (55 loc) · 1.37 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
67
68
69
70
71
72
73
74
75
76
//Memento
class Memento {
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
//Or*ginator
class Originator {
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Memento createMemento() {
return new Memento(state);
}
public void setMemento(Memento memento) {
state = memento.getState();
}
public void showState(){
System.out.println(state);
}
}
//Caretaker
class Caretaker {
private Memento memento;
public Memento getMemento(){
return this.memento;
}
public void setMemento(Memento memento){
this.memento = memento;
}
}
//
public class DS18_Memento{
public static void main(String[] args) {
Originator org = new Originator();
org.setState("开会中");
Caretaker ctk = new Caretaker ();
ctk.setMemento(org.createMemento());//将数据封装在Caretaker
org.setState("睡觉中");
org.showState();//显示
org.setMemento(ctk.getMemento());//将数据重新导入
org.showState();
}
}