2021.11.25
备忘录模式:多次撤销
改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。java源代码:
(1)Caretaker.java:
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
|
package shiyan20; import java.util.ArrayList; public class Caretaker { private ArrayList<Memento> mementos= new ArrayList<Memento>(); public Memento getMemento( int i) { return (Memento)mementos.get(i); } public void setMemento(Memento memento) { mementos.add(memento); } } |
(2)Client.java:
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
|
package shiyan20; public class Client { public static void main(String a[]){ UserInfoDTO user= new UserInfoDTO(); Caretaker taker = new Caretaker(); user.setAccount( "zhangsan" ); user.setPassword( "123456" ); user.setTelNo( "13000000000" ); System.out.println( "状态一:" ); taker.setMemento(user.saveMemento()); user.show(); System.out.println( "---------------------------" ); user.setPassword( "111111" ); user.setTelNo( "13100001111" ); System.out.println( "状态二:" ); user.show(); taker.setMemento(user.saveMemento()); System.out.println( "---------------------------" ); user.setPassword( "222222" ); user.setTelNo( "13100001111" ); System.out.println( "状态三:" ); user.show(); System.out.println( "---------------------------" ); System.out.println( "回到状态二" ); user.restoreMemento(taker.getMemento( 1 )); user.show(); System.out.println( "---------------------------" ); System.out.println( "回到状态一" ); user.restoreMemento(taker.getMemento( 0 )); user.show(); System.out.println( "---------------------------" ); } } |
(3)Memento.java:
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
|
package shiyan20; public class Memento { private String account; private String password; private String telNo; public Memento(String account,String password,String telNo) { this .account=account; this .password=password; this .telNo=telNo; } public String getAccount() { return account; } public void setAccount(String account) { this .account=account; } public String getPassword() { return password; } public void setPassword(String password) { this .password=password; } public String getTelNo() { return telNo; } public void setTelNo(String telNo) { this .telNo=telNo; } } |
(4)UserInfoDTO.java:
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package shiyan20; /* * 用户信息类 */ public class UserInfoDTO { private String account; private String password; private String telNo; public String getAccount() { return account; } public void setAccount(String account) { this .account=account; } public String getPassword() { return password; } public void setPassword(String password) { this .password=password; } public String getTelNo() { return telNo; } public void setTelNo(String telNo) { this .telNo=telNo; } public Memento saveMemento() { return new Memento(account,password,telNo); } public void restoreMemento(Memento memento) { this .account=memento.getAccount(); this .password=memento.getPassword(); this .telNo=memento.getTelNo(); } public void show() { System.out.println( "Account:" + this .account); System.out.println( "Password:" + this .password); System.out.println( "TelNo:" + this .telNo); } } |
实现截图: