备忘录模式
实验 20:备忘录模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解备忘录模式的动机,掌握该模式的结构;
2、能够利用备忘录模式解决实际问题。
[实验任务一]:多次撤销
改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。
类图
源代码
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import java.util.ArrayList; import java.util.List; public class Caretaker { private List<Memento> list= new ArrayList<>(); public Memento getMemento() { Memento mm=list.get(list.size()- 2 ); list.remove(list.size()- 2 ); return mm; } public void setMemento(Memento memento) { list.add(memento); } } public class Memento { 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(String account, String password, String telNo) { this .account = account; this .password = password; this .telNo = telNo; } } 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); } } public class Client { public static void main(String[] args) { // TODO Auto-generated method stub UserInfoDTO user= new UserInfoDTO(); Caretaker c= new Caretaker(); user.setAccount( "zhangsan" ); user.setPassword( "123456" ); user.setTelNo( "1310000000" ); System.out.println( "状态一:" ); user.show(); c.setMemento(user.saveMemento()); System.out.println( "-----------------------------" ); user.setPassword( "111111" ); user.setTelNo( "1310001111" ); System.out.println( "状态二:" ); user.show(); c.setMemento(user.saveMemento()); System.out.println( "-----------------------------" ); user.setPassword( "zyx666" ); user.setTelNo( "15733333333" ); System.out.println( "状态三:" ); user.show(); c.setMemento(user.saveMemento()); System.out.println( "-----------------------------" ); user.setPassword( "777777" ); user.setTelNo( "15511111111" ); System.out.println( "状态四:" ); user.show(); c.setMemento(user.saveMemento()); System.out.println( "-----------------------------" ); user.setPassword( "666666" ); user.setTelNo( "17455555555" ); System.out.println( "状态五:" ); user.show(); c.setMemento(user.saveMemento()); System.out.println( "-----------------------------" ); user.restoreMemento(c.getMemento()); System.out.println( "回到状态四:" ); user.show(); System.out.println( "-----------------------------" ); user.restoreMemento(c.getMemento()); System.out.println( "回到状态三:" ); user.show(); System.out.println( "-----------------------------" ); user.restoreMemento(c.getMemento()); System.out.println( "回到状态二:" ); user.show(); System.out.println( "-----------------------------" ); user.restoreMemento(c.getMemento()); System.out.println( "回到状态一:" ); user.show(); System.out.println( "-----------------------------" ); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用