6.21. Memo mode

发布时间 :2025-10-25 12:24:44 UTC      

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.

6.21.1. Introduction

意图: 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.

6.21.2. Realize

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.

备忘录模式的 UML 图

6.21.3. Step 1

Create the Memento class.

Memento.java

publicclassMemento{privateStringstate;publicMemento(Stringstate){this.state=state;}publicStringgetState(){returnstate;}}

6.21.4. Step 2

Create the Originator class.

Originator.java

publicclassOriginator{privateStringstate;publicvoidsetState(Stringstate){this.state=state;}publicStringgetState(){returnstate;}publicMementosaveStateToMemento(){returnnewMemento(state);}publicvoidgetStateFromMemento(MementoMemento){state=Memento.getState();}}

6.21.5. Step 3

Create the CareTaker class.

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

Use CareTaker And Originator Object.

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

Verify the output.

Current State: State #4
First saved State: State #2
Second saved State: State #3
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.