备忘录模式——C++实现
问题描述:
改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用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 | #include<iostream> #include<string> #include<vector> using namespace std; //备忘录 class Memento { private : string account; string password; string telNo; public : Memento(string account, string password, string telNo) { this ->account = account; this ->password = password; this ->telNo = telNo; } string getAccount() { return account; } void setAccount(string account) { this ->account = account; } string getPassword() { return password; } void setPassword(string password) { this ->password = password; } string getTelNo() { return telNo; } void setTelNo(string telNo) { this ->telNo = telNo; } }; //发起者 class UserInfoDTO { private : string account; string password; string telNo; public : string getAccount() { return account; } void setAccount(string account) { this ->account = account; } string getPassword() { return password; } void setPassword(string password) { this ->password = password; } string getTelNo() { return telNo; } void setTelNo(string telNo) { this ->telNo = telNo; } Memento saveMemento() { Memento memento(account, password, telNo); return memento; } void restoreMenmento(Memento memento) { this ->account = memento.getAccount(); this ->password = memento.getPassword(); this ->telNo = memento.getTelNo(); } void show() { cout << "帐号:" + this ->account << endl; cout << "密码:" + this ->password << endl; cout << "电话:" + this ->telNo << endl; } }; //管理者 class Caretaker { private : vector<Memento>mementos; public : Memento getMemento( int i) { return mementos.at(i); } void setMemento(Memento memento) { mementos.push_back(memento); } }; //测试函数 int main() { int index = -1; UserInfoDTO user; Caretaker taker; user.setAccount( "张三" ); user.setPassword( "123456" ); user.setTelNo( "18800000000" ); cout << "状态一:" << endl; taker.setMemento(user.saveMemento()); index++; user.show(); cout << "----------------------------------" << endl; user.setPassword( "111111" ); user.setTelNo( "18800001111" ); cout << "状态二:" << endl; taker.setMemento(user.saveMemento()); index++; user.show(); cout << "----------------------------------" << endl; user.setPassword( "222222" ); user.setTelNo( "18800002222" ); cout << "状态三:" << endl; user.show(); cout << "----------------------------------" << endl; cout << "回到状态二:" << endl; index--; user.restoreMenmento(taker.getMemento(1)); user.show(); cout << "----------------------------------" << endl; cout << "回到状态一:" << endl; index --; user.restoreMenmento(taker.getMemento(0)); user.show(); cout << "----------------------------------" << endl; } |
运行截图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下