Memento mode in Behavioral mode
1. Intention
Capture the internal state of an object without damaging the encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.
2. Alias
Token
3. Motivation
Sometimes it is necessary to record the internal status of an object. In order to allow users to cancel uncertain operations or recover from errors, check points and cancellation mechanisms must be implemented. To implement these mechanisms, you must save the status information somewhere in advance, in this way, objects can be restored to their previous state.
4. Applicability
Memento mode is used in the following cases:
You must save the (partial) state of an object at a certain time point so that it can be restored to the previous state later. If an interface is used to directly obtain these States for other objects, the implementation details of the object will be exposed and the object encapsulation will be broken.
5. Structure
The main purpose is to save A certain state of an object so that the object can be restored at an appropriate time. I personally think that the backup mode is more vivid. In general, assume that there is an original Class, A has various attributes. A can decide the attributes to be backed up. Memorandum Class B is used to store some internal states of A. Class C is used to store memos and can only be stored, cannot be modified. Make a picture to analyze:
6. Sample Code
The Original class is the Original class. It contains the attribute value to be saved and creates a memorandum class to save the value. The Memento class is the memorandum class, and the Storage class is the Storage memorandum class. It is easy to understand this mode because it holds instances of the Memento class. View the source code directly:
Implementation Code:
Data structure:
public class Original { private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public Original(String value) { this.value = value; } public Memento createMemento(){ return new Memento(value); } public void restoreMemento(Memento memento){ this.value = memento.getValue(); }}
Implementation:
public class Memento { private String value; public Memento(String value) { this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; }}public class Storage { private Memento memento; public Storage(Memento memento) { this.memento = memento; } public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; }}
Test class:
Public class Test {
Public static void main (String [] args) {// create the Original Class Original origi = new Original (egg); // create a memorandum Storage storage = new Storage (origi. createMemento (); // modify the status of the original class System. out. println (initialization status: + origi. getValue (); origi. setValue (niu); System. out. println (the modified status is + origi. getValue (); // returns the original class status origi. restoreMemento (storage. getMemento (); System. out. println (the restored status is + origi. getValue ());}
}
''
Output:
Initialization status: egg
The modified status is niu.
The restored status is egg.
Simple Description: when the original class is created, the value is initialized to egg. After modification, the value of the value is set to niu, And the last and second rows are restored. The result is successfully restored. In fact, I think this mode is the most vivid.
7. Related ModesCommand mode: In Command mode, you can use memos to maintain the status of Undo operations. Iterator mode: As described earlier, memos can be used for iteration.