Entity classes are used to store information and define methods for backup and restoration

Public class Student {private String sname; private String sname; private String sid; private String className; // Constructor public Student(String sname, String sid, String className) {super(); this.sname = sname; this.sid = sid; this.className = className; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } @override public String toString() {Override public String toString() {Override public String toString(); } public Memento Memento() {return new Memento(this); } public void recovery(Memento m) {this.sname= m.getsname (); this.sid=m.getSid(); this.className=m.getClassName(); }}Copy the code

Memo class, save student entity class information

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; Public class Memento {private String sname; private String sid; private String className; Public Memento(Student s) {super(); this.sname =s.getSname(); this.sid = s.getSid(); this.className = s.getClassName(); Properties p=new Properties(); P.setproperty (" student name ", sname); P.setproperty (" student id ", SID); P.setproperty (" className ", className); Try {p.storetoxml (new FileOutputStream(new File(" File 3.xml")), "test File ","GBK"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; }}Copy the code

The manager class, which keeps references to the memo class, can use a List container to hold multiple memo points

Public class Manager {// Keep references to Memento Memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; }}Copy the code

The client class

public class Client { public static void main(String[] args) { Manager m=new Manager(); Student s=new Student(" 三","123"," 三"); System.out.println(s.tostring ())); // backup system.out.println ("--> backup --"); m.setMemento(s.memento()); // reset data s.setsname (" li Si "); s.setSid("456"); S. SetclassName (" Software Class 2 "); System.out.println("--> data changed ----"); System.out.println(s.toString()); System.out.println("--> back to ----"); s.recovery(m.getMemento()); System.out.println(s.toString()); }}Copy the code