6.21. Memo mode

发布时间 : 2025-10-25 13:34:16 UTC      

Page Views: 9 views

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 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。