definition: saves a state of an object so that it can be recovered at the appropriate time
Characteristics:
1, to provide users with a mechanism to restore the state, you can make it easier for users to return to a certain historical state.
2, the implementation of information encapsulation, so that users do not need to care about the state of the Save details.
Enterprise-class applications and applications in common frameworks: a common text editor uses this pattern
Instance:
Note: There is only a undo operation in this instance, there is no forward restore operation
/** * Target object: Object to be remembered/class Word {private String content;
Private String image;
Private String table;
Public Word (string content, string image, String table) {super ();
this.content = content;
This.image = image;
this.table = table;
Public Wordmemento Memento () {Return to New Wordmemento (this);
public void Recovery (Wordmemento memento) {this.content = Memento.getcontent ();
This.image = Memento.getimage ();
this.table = Memento.gettable ();
Public String getcontent () {return content;
public void SetContent (String content) {this.content = content;
Public String GetImage () {return image;
public void SetImage (String image) {this.image = image;
Public String gettable () {return table;
public void settable (String table) {this.table = table;
}/** * Memo object/class wordmemento{private String content;
Private String image;
Private String table; Public Wordmemento (Word word) {this.content = Word.getcontent ();
This.image = Word.getimage ();
this.table = Word.gettable ();
Public String getcontent () {return content;
public void SetContent (String content) {this.content = content;
Public String GetImage () {return image;
public void SetImage (String image) {this.image = image;
Public String gettable () {return table;
public void settable (String table) {this.table = table;
}/** * Owner object: Responsible for record Memo object/class caretaker{private list<wordmemento> List = new arraylist<> ();
private int index = 0;
public void Setmemento (Wordmemento memento) {List.add (memento);
This.index = List.size ();
Public Wordmemento Getwordmemento () {if (index = = 0) {System.out.println ("No content to restore");
return null;
} Wordmemento memento = List.get (index-1);
List.remove (index-1);
index--;
return memento; }
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.