/** * Data Object * @author Stone * */public class DataState {private string action;public void Setaction (String action) {This.ac tion = action;} Public String getaction () {return action;}}
/** * An object that holds a copy of the internal state of another object. You can then restore the object to its original saved state. * * @author stone * */import Java.io.file;import Java.io.serializable;public class Memento implements Serializable {/ *private int number;private File File = null;public Memento (originator o) {this.number = O.getnumber (); this.file = O.getfi Le ();} public int GetNumber () {return this.number;} public void Setnumber (int number) {this.number = number;} Public File GetFile () {return this.file;} public void Setfile (file file) {this.file = file;} */private datastate state;public Memento (originator o) {this.state = O.getstate ();} Public DataState GetState () {return state;} public void SetState (DataState state) {this.state = state;}}
public class originator {/*private int number;private file File = null;public originator () {}//creates a Memento that passes itself as a parameter to public Memento Getmemento () {return new Memento (this);} Remove saved data from Memento, revert to original state public void Setmemento (Memento m) {number = M.getnumber (); file = M.getfile ();} public int GetNumber () {return number;} public void Setnumber (int number) {this.number = number;} Public file GetFile () {return file;} public void Setfile (file file) {this.file = file;} */private datastate state;public Originator () {}public originator (datastate state) {this.state = state;} Create a Memento, pass itself as a parameter to public Memento Getmemento () {return new Memento (this);} Remove the saved data from the Memento, revert to the original state public void Setmemento (Memento m) {/* * Getmemento () object created, save in a container, * when recovery is needed, pass it to the current method, and then use get State (), Draw */this.state = M.getstate ();} Public DataState GetState () {return state;} public void SetState (DataState state) {this.state = state;}}
/* Memo (Memento) mode behavioral mode * Flow: Memento is used to hold data state, * originator is used to load data, establish Memento object, and restore raw data Memento class Test {p Ublic static void Main (string[] args) {//originator originator = new originator ();//originator.setnumber (8);////memento Memento = Originator.getmemento ();//system.out.println (Memento.getnumber ());D atastate state = new DataState (); State.setaction ("Copy a character"); originator originator = new originator (); SYSTEM.OUT.PRINTLN ("Create raw Data"); Originator.setstate (state); SYSTEM.OUT.PRINTLN ("Create Memo object, save raw Data State"); Memento Memento = Originator.getmemento (); System.out.println ("created a new data"); Originator.setstate (newly datastate ()); System.out.println ("After creating new data:" + originator.getstate (). Getaction ());/* * Memento need to be saved somewhere and taken out when needed to restore the data stored inside it */ System.out.println ("restore the original data after creating new data"); Originator.setmemento (Memento); System.out.println (Originator.getstate (). Getaction ());}}
Print
Create the original data create a Memo object, save the original data state create a new data after creating new data: null After creating new data, restore the original data copy a character
Java Implementation Memo (Memento) mode