软件设计20|备忘录模式
一、Java版本
(1)效果如下:
(2)类图如下:
(3)工程目录如下:
(4)代码如下:
Caretaker.java
1 package memo; 2 3 public class Caretaker { 4 private Memento memento; 5 6 public void setMemento(Memento memento) { 7 this.memento = memento; 8 } 9 10 public Memento getMemento() { 11 // TODO Auto-generated method stub 12 return memento; 13 } 14 15 }
Client.java
1 package memo; 2 3 public class Client { 4 public static void main(String[] args) { 5 UserInfoDTO user=new UserInfoDTO(); 6 Caretaker c=new Caretaker(); 7 Caretaker d=new Caretaker(); 8 user.setAccount("张三"); 9 user.setPassword("123456"); 10 user.setTelNo("123456789"); 11 System.out.println("状态一:"); 12 user.show(); 13 c.setMemento(user.saveMemento());//保存到备忘录中 14 System.out.println("************"); 15 user.setPassword("11111"); 16 user.setTelNo("321"); 17 System.out.println("状态二:"); 18 user.show(); 19 d.setMemento(user.saveMemento());//保存到备忘录中 20 System.out.println("************"); 21 user.setPassword("22222"); 22 user.setTelNo("66666"); 23 System.out.println("状态三:"); 24 user.show(); 25 System.out.println("************"); 26 user.restoreMemento(d.getMemento());//从备忘录中恢复 27 System.out.println("回到状态二:"); 28 user.show(); 29 System.out.println("************"); 30 user.restoreMemento(c.getMemento());//从备忘录中恢复 31 System.out.println("回到状态一:"); 32 user.show(); 33 } 34 }
Memento.java
1 package memo; 2 3 public class Memento { 4 private String account; 5 private String password; 6 private String telNo; 7 8 public Memento(String account,String password,String telNo) { 9 this.account=account; 10 this.password=password; 11 this.telNo=telNo; 12 } 13 14 public String getAccount() { 15 return account; 16 } 17 public void setAccount(String account) { 18 this.account = account; 19 } 20 public String getPassword() { 21 return password; 22 } 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 public String getTelNo() { 27 return telNo; 28 } 29 public void setTelNo(String telNo) { 30 this.telNo = telNo; 31 } 32 33 }
UserInfoDTO.java
1 package memo; 2 3 public class UserInfoDTO { 4 private String account; 5 private String password; 6 private String telNo; 7 8 public Memento saveMemento() { 9 return new Memento(account, password, telNo); 10 } 11 12 public void restoreMemento(Memento memento){ 13 this.account=memento.getAccount(); 14 this.password=memento.getPassword(); 15 this.telNo=memento.getTelNo(); 16 } 17 18 public void show() { 19 System.out.println("Account:"+this.account); 20 System.out.println("Password:"+this.password); 21 System.out.println("TelNo:"+this.telNo); 22 } 23 24 public String getAccount() { 25 return account; 26 } 27 28 public void setAccount(String account) { 29 this.account = account; 30 } 31 32 public String getPassword() { 33 return password; 34 } 35 36 public void setPassword(String password) { 37 this.password = password; 38 } 39 40 public String getTelNo() { 41 return telNo; 42 } 43 44 public void setTelNo(String telNo) { 45 this.telNo = telNo; 46 } 47 }
(5)参考链接:设计模式十七
二、C++版本
(1)效果如下:
(2)类图如下:
(3)代码如下:
1 #include<iostream> 2 using namespace std; 3 4 class Memento 5 { 6 public: 7 string account; 8 string password; 9 string telNo; 10 public: 11 Memento(){} 12 Memento(string account, string password, string telNo) :account(account), password(password), telNo(telNo) {} 13 string getAccount() { 14 return account; 15 } 16 void setAccount(string a) { 17 account = a; 18 } 19 string getPassword() { 20 return password; 21 } 22 void setPassword(string p) { 23 password = p; 24 } 25 string getTelNo() { 26 return telNo; 27 } 28 void setTelNo(string t) { 29 telNo = t; 30 } 31 32 }; 33 34 class UserInfoDTO 35 { 36 private: 37 string account; 38 string password; 39 string telNo; 40 public: 41 Memento SaveMemento() 42 { 43 Memento memento(account, password, telNo); 44 return memento; 45 } 46 void restoreMemento(Memento memento) 47 { 48 account = memento.account; 49 password = memento.password; 50 telNo = memento.telNo; 51 } 52 void Show() { 53 cout << "account : " << account << endl; 54 cout << "password: " << password << endl; 55 cout << "telNo: " << telNo << endl; 56 } 57 string getAccount() { 58 return account; 59 } 60 void setAccount(string a) { 61 account = a; 62 } 63 string getPassword() { 64 return password; 65 } 66 void setPassword(string p) { 67 password = p; 68 } 69 string getTelNo() { 70 return telNo; 71 } 72 void setTelNo(string t) { 73 telNo = t; 74 } 75 }; 76 //保存的进度库 77 class Caretake 78 { 79 public: 80 Caretake() {} 81 void setMemento(Memento menento) { 82 m_memento = menento; 83 } 84 Memento getMemento() { return m_memento; } 85 private: 86 Memento m_memento; 87 }; 88 89 int main() 90 { 91 UserInfoDTO user; 92 user.setAccount("张三"); 93 user.setPassword("11111"); 94 user.setTelNo("111111"); 95 cout << "状态一:" << endl; 96 user.Show(); 97 Caretake* c=new Caretake(); 98 c->setMemento(user.SaveMemento());//保存到备忘录 99 cout << "**************" << endl; 100 user.setPassword("22222"); 101 user.setTelNo("222222"); 102 cout << "状态二:" << endl; 103 user.Show(); 104 Caretake* d = new Caretake(); 105 d->setMemento(user.SaveMemento());//保存到备忘录 106 cout << "**************" << endl; 107 user.setPassword("33333"); 108 user.setTelNo("333333"); 109 cout << "状态三:" << endl; 110 user.Show(); 111 cout << "**************" << endl; 112 user.restoreMemento(d->getMemento());//从备忘录中恢复 113 cout << "回到状态二:" << endl; 114 user.Show(); 115 cout << "**************" << endl; 116 user.restoreMemento(c->getMemento());//从备忘录中恢复 117 cout << "回到状态一:" << endl; 118 user.Show(); 119 return 0; 120 }
(4)参考链接:设计模式C++实现(12)——备忘录模式