Java memo mode
Memo pattern saves a certain state of an object so that the object can be restored at an appropriate time. Memo mode belongs to behavior mode.
**Intent: * * capture the internal state of an object and save the state outside the object without destroying the encapsulation.
**Main solutions: * * the so-called memo mode is to capture the internal state of an object without destroying the encapsulation, and save the state outside the object, so that the object can be restored to the original saved state in the future.
Application examples: 1. Regret medicine. 2. Archive when playing games. 3. ctri + z in Windows. 4. Backward in IE. 4. Transaction management of database.
Advantages: 1. It provides users with a mechanism to restore the state, so that users can easily return to a historical state. 2. 2. The encapsulation of information is realized, so that users do not need to care about the details of state preservation.
**Disadvantages: * * resource consumption. If there are too many member variables of a class, it is bound to occupy a large amount of resources, and each save will consume a certain amount of memory.
Usage scenarios: 1. Relevant state scenarios that need to save / restore data. 2. Provides a rollback operation.
Notes: 1. In order to comply with the Demeter principle, a management memorandum class should be added. 2. In order to save memory, prototype mode + memo mode can be used.
The forgetting mode uses three classes Memento, Originator, and CareTaker
Memento contains the state of the object to be restored.
public class Memento { private String state; public Memento(String state){ this.state = state; } public String getState(){ return state; } }
The Originator creates and stores the state in the Memento object.
public class Originator { private String state; public void setState(String state){ this.state = state; } public String getState(){ return state; } public Memento saveStateToMemento(){ return new Memento(state); } public void getStateFromMemento(Memento Memento){ state = Memento.getState(); } }
The Caretaker object is responsible for restoring the state of the object from Memento.
public class CareTaker { private List<Memento> mementoList = new ArrayList<Memento>(); public void add(Memento state){ mementoList.add(state); } public Memento get(int index){ return mementoList.get(index); } }
test
public class MementoPatternDemo { public static void main(String[] args) { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.setState("State #1"); originator.setState("State #2"); careTaker.add(originator.saveStateToMemento());//Save files originator.setState("State #3"); careTaker.add(originator.saveStateToMemento()); //Save files originator.setState("State #4"); System.out.println("current state : " + originator.getState()); originator.getStateFromMemento(careTaker.get(0));//Get Archive System.out.println("Restore to archive 1 state: " + originator.getState()); originator.getStateFromMemento(careTaker.get(1)); System.out.println("Restore to archive 2 State: " + originator.getState()); } }
Current state: State #4
Restore to archive 1 state: State #2
Restore to archive 2 State: State #3