Usually, when the word document editor suddenly loses power to the computer, when we open it again, the original record status is still.
ps software history, we can use this record to restore the previous actions. These are the scenarios of memo mode
Memo mode is to provide a function to save the state of an object, so that the object can be restored to its original state later
Structure:
Originator class: it is responsible for creating a memo to record the internal state of itself at the current time, and can use the memo to restore the internal state.
Memo class Memento: responsible for storing the internal state of the Originator object
Responsible for human CareTake: management memo class
/** * Source generator class * */ public class Emp { //Object state private String name; private int age; private double salary; public Emp(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } //Create memo object and make memo public EmpMemento empMemento() { return new EmpMemento(this); } //Restore data public void recovery(EmpMemento emp){ this.name = emp.getName(); this.age = emp.getAge(); this.salary = emp.getSalary(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Emp{" + "name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}'; } }
/** * Memorandum class */ public class EmpMemento { private String name; private int age; private double salary; public EmpMemento(Emp e) { this.name = e.getName(); this.age = e.getAge(); this.salary = e.getSalary(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
/** * Responsible for human beings * Management memo object */ public class CareTaker { //list ...or private EmpMemento memento; public EmpMemento getMemento() { return memento; } public void setMemento(EmpMemento memento) { this.memento = memento; } }
Here, if you want to record and save multiple times (multiple note points), you can wrap the attribute EmpMemento in a container.
public class Client { public static void main(String[] args) { CareTaker taker = new CareTaker(); //Creating objects for the first time Emp emp = new Emp("test", 18, 1000); System.out.println(emp);
//Save once status taker.setMemento(emp.empMemento()); emp.setAge(99); System.out.println(emp); //Recovery state emp.recovery(taker.getMemento()); System.out.println(emp); } }
Emp{name='test', age=18, salary=1000.0}
Emp{name='test', age=99, salary=1000.0}
Emp{name='test', age=18, salary=1000.0}
Process finished with exit code 0
Data recovery is successful, that is, it is successful when saving the state.
Commonly used in development:
Chess and card games, regret operation
Software undo steps
Transaction management rollback, etc