具体代码
//Memento.java
package org.example.test018;
public class Memento {
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;
}
private String account;
private String password;
private String telNo;
}
//UserInfoDTO.java
package org.example.test018;
public class UserInfoDTO {
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;
}
private String account;
private String password;
private String 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);
}
}
//Caretaker.java
package org.example.test018;
public class Caretaker {
private Memento[] array = null;
private int index;
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
public Caretaker() {
array = new Memento[10];
index = 0;
}
public void addMemento(Memento Memento) {
array[index++] = Memento;
}
public Memento preMemento() {
Memento pop = null;
try {
pop = array[--index];
}
catch (Exception e) {
e.printStackTrace();
}
return pop;
}
}
//Client.java
package org.example.test018;
import javax.security.auth.login.CredentialException;
public class Client {
public static void main(String[] args) {
UserInfoDTO userInfoDTO = new UserInfoDTO();
Caretaker caretaker = new Caretaker();
userInfoDTO.setAccount("zhangsan");
userInfoDTO.setPassword("123456");
userInfoDTO.setTelNo("1300000000");
System.out.println("状态一:");
userInfoDTO.show();
caretaker.addMemento(userInfoDTO.saveMemento());
System.out.println("------------------");
userInfoDTO.setPassword("111111");
userInfoDTO.setTelNo("13000000001");
System.out.println("状态二:");
userInfoDTO.show();
caretaker.addMemento(userInfoDTO.saveMemento());
System.out.println("-------------------");
userInfoDTO.setPassword("222222");
userInfoDTO.setTelNo("12222222222");
System.out.println("状态三:");
userInfoDTO.show();
System.out.println("-------------------");
userInfoDTO.restoreMemento(caretaker.preMemento());
System.out.println("回到状态二:");
userInfoDTO.show();
System.out.println("-------------------");
userInfoDTO.restoreMemento(caretaker.preMemento());
System.out.println("回到状态一:");
userInfoDTO.show();
System.out.println("-------------------");
}
}