Memo mode (Memento Pattern) saves a state of an object so that it can be restored at an appropriate time. The memo model belongs to the behavioral model. 意图: Without breaking the encapsulation, capture the internal state of an object and save that state outside the object. 主要解决: The so-called memo mode is to capture the internal state of an object without breaking the encapsulation, and save the state outside the object, so that the object can be restored to the previously saved state later. 何时使用: In many cases, we always need to record the internal state of an object. the purpose of this is to allow the user to cancel uncertain or erroneous operations, return to his original state, and give him “medicine for regret”. 如何解决: The object state is stored specifically through a memo class. 关键代码: The customer is not coupled to the memo class, but to the memo management class. 应用实例: 1. Regret medicine. 2. Archiving when playing games. 3. Ctrl + z in Windows. 4. Retreat in IE. 5. Transaction management of database. 优点: 1. Provide the user with a mechanism that can restore the state, so that the user can easily return to a certain historical state. 2. The encapsulation of the information is realized, so that the user does not need to care about the details of the state. 缺点: Consume resources. 使用场景: 1. Relevant state scenarios where you need to save / restore data. 2. Provide a rollback operation. 注意事项: 1. In order to comply with the Dimitt principle, a class for managing memos should be added. 2. To save memory, prototype mode + memo mode can be used. The memo mode uses three classes Memento 、 Originator And CareTaker . The Memento contains the state of the object to be restored. Originator creates and stores state in a Memento object. The Caretaker object is responsible for restoring the state of the object from the Memento. MementoPatternDemo Our demo class uses the CareTaker And Originator Object to display the state recovery of the object. Create the Memento class. Create the Originator class. Create the CareTaker class. Use CareTaker And Originator Object. Verify the output. 6.21.1. Introduction ¶
6.21.2. Realize ¶

6.21.3. Step 1 ¶
Memento.java ¶
publicclassMemento{privateStringstate;publicMemento(Stringstate){this.state=state;}publicStringgetState(){returnstate;}}
6.21.4. Step 2 ¶
Originator.java ¶
publicclassOriginator{privateStringstate;publicvoidsetState(Stringstate){this.state=state;}publicStringgetState(){returnstate;}publicMementosaveStateToMemento(){returnnewMemento(state);}publicvoidgetStateFromMemento(MementoMemento){state=Memento.getState();}}
6.21.5. Step 3 ¶
CareTaker.java ¶
importjava.util.ArrayList;importjava.util.List;publicclassCareTaker{privateList<Memento>mementoList=newArrayList<Memento>();publicvoidadd(Mementostate){mementoList.add(state);}publicMementoget(intindex){returnmementoList.get(index);}}
6.21.6. Step 4 ¶
MementoPatternDemo.java ¶
publicclassMementoPatternDemo{publicstaticvoidmain(String[]args){Originatororiginator=newOriginator();CareTakercareTaker=newCareTaker();originator.setState("State
#1");originator.setState("State
#2");careTaker.add(originator.saveStateToMemento());originator.setState("State
#3");careTaker.add(originator.saveStateToMemento());originator.setState("State
#4");System.out.println("Current
State:"+originator.getState());originator.getStateFromMemento(careTaker.get(0));System.out.println("First
saved
State:"+originator.getState());originator.getStateFromMemento(careTaker.get(1));System.out.println("Second
saved State:"+originator.getState());}}
6.21.7. Step 5 ¶
Current State: State #4
First saved State: State #2
Second saved State: State #3