http://alaric.iteye.com/blog/1931253
Http://www.cnblogs.com/java-my-life/archive/2012/06/06/2534942.html
1. Memo Mode (Memento)
Definition: The main purpose is to save a state of an object in order to restore the object at the appropriate time, and the individual feels that the backup mode is more image.
In layman's words: Assuming there are various attributes in the original class A,a, a can determine the attributes that need to be backed up, and Memo Class B is used to store
A some of the internal state, Class C, is a used to store the memo, and can only be stored, can not be modified and other operations
Diagram:
2. For example
2.1 Original class: Primitive class:
Original classes public class Original {//state information private String state= ""; Memo: Save status information Public Memento Creatememento () {return new Memento (state);} Public String GetState () {return state;} Reset status information: Resets the public void ResetState (Memento Memento) { System.out.print ("Reset-") from the memo, SetState (Memento.getstate ()) ;} public void SetState (String state) {this.state = state; System.out.println ("Set-state:" +state);}} Memo: Non-modifiable status information class Memento{private string State=null;public string GetState () {return state;} Public Memento (String state) {this.state=state;}}
2.2 Storage classes:
Save the memo storage class public class Storage {private Memento memento=null;public Storage (Memento Memento) {This.memento=memento;} Public Memento Getmemento () {return Memento;}}
2.3 Test:
public class Test {public static void main (string[] args) {//TODO auto-generated method stub Original ori=new Origina L (); Ori.setstate ("Off"); Save Memo Storage sto=new Storage (Ori.creatememento ()); Change the status Ori.setstate ("on"); Reset Status ori.resetstate (Sto.getmemento ());}}
2.4 Operation Result:
Set-Status: Off Setting-Status: On reset-Settings-Status: Off
3. Black-Box operation (the memo is defined in the form of a privatized internal class and then accessed through an interface):
Original classes public class Original {//state information private String state= ""; Memo: Save information in the form of an internal class, inherit the interface, make it easy for outsiders to access it private class Memento implements Mementoif{public Memento (Original ori) {state= Ori.state;} Public String getState () {//TODO auto-generated method Stubreturn null;}} Public Memento Creatememento () {Return to New Memento (this);} Public String GetState () {return state;} Reset status information: Resets the public void ResetState (mementoif Memento) { System.out.print ("Reset-") from the memo; Memento mem= (Memento) Memento; SetState (Mem.getstate ());} public void SetState (String state) {this.state = state; System.out.println ("Set-state:" +state);}} Create an interface: facilitates external access to the inner class of the memo interface Mementoif {}
Test:
public class Test {public static void main (string[] args) {//TODO auto-generated method stub Original ori=new Origina L (); Ori.setstate ("Off"); Save Memo Mementoif Memento=ori.creatememento (); Change the status Ori.setstate ("on"); Reset Status ori.resetstate (memento);} }
Results:
Set-Status: Off Setting-Status: On reset-Settings-Status: Off
java-design mode (behavioral)-"Memo mode"