备忘录模式
题目:
改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。
java实现
package memento; 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; } }
package memento; 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); } }
package memento; import java.util.ArrayList; import java.util.List; public class Caretaker { private List<Memento> list = new ArrayList<>(); public Memento getMemento(){ Memento m = list.get(list.size()-1); list.remove(list.size()-1); return m; } public void setMemento(Memento m){ list.add(m); } }
package memento; public class Client { public static void main(String[] args) { UserInfoDTO user = new UserInfoDTO(); Caretaker caretaker = new Caretaker(); user.setAccount("张三"); user.setPassword("123456"); user.setTelNo("11111111111"); System.out.println("状态一:"); user.show(); caretaker.setMemento(user.saveMemento()); System.out.println("----------------------------------"); user.setPassword("222222"); user.setTelNo("11111112222"); System.out.println("状态二"); user.show(); caretaker.setMemento(user.saveMemento()); System.out.println("----------------------------------"); user.setPassword("333333"); user.setTelNo("11111113333"); System.out.println("状态三"); user.show(); caretaker.setMemento(user.saveMemento()); System.out.println("----------------------------------"); user.setPassword("444444"); user.setTelNo("11111114444"); System.out.println("状态四"); user.show(); caretaker.setMemento(user.saveMemento()); System.out.println("----------------------------------"); user.setPassword("555555"); user.setTelNo("11111115555"); System.out.println("状态五"); user.show(); caretaker.setMemento(user.saveMemento()); System.out.println("----------------------------------"); user.restoreMemento(caretaker.getMemento()); System.out.println("回到状态五:"); user.show(); System.out.println("----------------------------------"); user.restoreMemento(caretaker.getMemento()); System.out.println("回到状态四:"); user.show(); System.out.println("----------------------------------"); user.restoreMemento(caretaker.getMemento()); System.out.println("回到状态三:"); user.show(); System.out.println("----------------------------------"); user.restoreMemento(caretaker.getMemento()); System.out.println("回到状态二:"); user.show(); System.out.println("----------------------------------"); user.restoreMemento(caretaker.getMemento()); System.out.println("回到状态一:"); user.show(); System.out.println("----------------------------------"); } }
c++
#include<iostream> #include<list> using namespace std; class Memento { private: string account; string pwd; string tel; public: string getAccount() { return account; } void setAccount(string account) { this->account = account; } string getPwd() { return pwd; } void setPwd(string pwd) { this->pwd = pwd; } string getTel() { return tel; } void setTel(string tel) { this->tel = tel; } Memento(string account, string pwd, string tel) { this->account = account; this->pwd = pwd; this->tel = tel; } }; class UserInfo { private: string account; string pwd; string tel; public: string getAccount() { return account; } void setAccount(string account) { this->account = account; } string getPwd() { return pwd; } void setPwd(string pwd) { this->pwd = pwd; } string getTel() { return tel; } void setTel(string tel) { this->tel = tel; } Memento *saveMemento() { return new Memento(account, pwd, tel); } void restoreMemento(Memento *memento) { this->account = memento->getAccount(); this->pwd = memento->getPwd(); this->tel = memento->getTel(); } void show() { cout << "account:" << this->account << endl; cout << "pwd:" << this->pwd << endl; cout << "tel:" << this->tel << endl; } }; class Caretaker { private: list<Memento *> list; public: Memento *getMemento() { list.pop_front(); Memento *m = list.front(); return m; } void setMemento(Memento *m) { list.push_front(m); } }; int main() { UserInfo *user = new UserInfo(); Caretaker *c = new Caretaker(); user->setAccount("张三"); user->setPwd("123456"); user->setTel("1310000000"); cout<<"状态一:"<<endl; user->show(); c->setMemento(user->saveMemento()); cout<<"-----------------------------"<<endl; user->setPwd("111111"); user->setTel("1310001111"); cout<<"状态二:"<<endl; user->show(); c->setMemento(user->saveMemento()); cout<<"-----------------------------"<<endl; user->setPwd("zyx666"); user->setTel("15733333333"); cout<<"状态三:"<<endl; user->show(); c->setMemento(user->saveMemento()); cout<<"-----------------------------"<<endl; user->setPwd("777777"); user->setTel("15511111111"); cout<<"状态四:"<<endl; user->show(); c->setMemento(user->saveMemento()); cout<<"-----------------------------"<<endl; user->setPwd("666666"); user->setTel("17455555555"); cout<<"状态五:"<<endl; user->show(); c->setMemento(user->saveMemento()); cout<<"-----------------------------"<<endl; user->restoreMemento(c->getMemento()); cout<<"回到状态四:"<<endl; user->show(); cout<<"-----------------------------"<<endl; user->restoreMemento(c->getMemento()); cout<<"回到状态三:"<<endl; user->show(); cout<<"-----------------------------"<<endl; user->restoreMemento(c->getMemento()); cout<<"回到状态二:"<<endl; user->show(); cout<<"-----------------------------"<<endl; user->restoreMemento(c->getMemento()); cout<<"回到状态一:"<<endl; user->show(); cout<<"-----------------------------"<<endl; return 0; }