The so-called memo pattern is to capture the internal state of an object without destroying the package, and to save the state outside of the object, so that the object can be restored to its previously saved state at a later time.
Personal Understanding
Memo mode is a pattern that can be used to temporarily restore an object's state in some special cases, and can be implemented in a variety of ways, including clones and general methods, as well as memos of various parameters. The standard memo is difficult to apply directly in the project, most of it is after the deformation of the processing mode.
Memo Mode role
The memo pattern mainly includes the following roles:
Originator: the original generator. is responsible for creating a memo that records the internal state of the current object, which can also be used to restore the internal state using the memo. At the same time, the original generator can also decide the internal state of memento storage originator as needed.
Memento: Memo. Used to store the internal state of the originator, and can prevent objects other than originator from accessing the memento. There are two interfaces in memo memento, where caretaker can only see the narrow interface in the memo, and it can only pass the memo to other objects. Originator can see a wide interface, allowing it to access all data returned to its previous state.
Caretaker: the person in charge. Be responsible for saving memos, not manipulating and accessing the contents of the memo, but only passing notes to other objects.
Case Analysis
Case One: General Memo mode
When there is only one parameter in the object, use the memo mode, save the current state, by the memo class to be staged, and the memo object to be managed by the administrator, when the need for recovery, directly using the memo's administrator to get the memo, set the current object to restore the value of the property values can be.
Main code
The Administrator of the memo
public class Caretaker {private Memento memento;public Memento Getmemento () {return Memento;} public void Setmemento (Memento Memento) {this.memento = Memento;}}
Memo Class
public class Memento {private String state = ' ";p ublic Memento (String state) {this.state = state;} Public String GetState () {return state;} public void SetState (String state) {this.state = state;}}
Classes to be prepared for memo processing
public class Originator {private String state = "";p ublic string getState () {return this.state;} public void SetState (String state) {this.state = state;} Public Memento Creatememento () {return new Memento (this.state);} public void Backstate (Memento Memento) {this.setstate (Memento.getstate ());}}
Test class
public class Maintest {public static void main (string[] args) {Originator orig = new originator (); Orig.setstate ("very good"); System.out.println (Orig.getstate ()); Caretaker taker = new Caretaker (); Taker.setmemento (Orig.creatememento ()); Orig.setstate ("bad"); System.out.println (Orig.getstate ()); Orig.backstate (Taker.getmemento ()); System.out.println (Orig.getstate ());}}
Case TWO: Clone implementation Memo mode
The role of the original and memo roles is fused into a single role, which is achieved by cloning the object itself for the purpose of backing up the object. When recovery is required, the values in the copy of the cloned object are taken out and set to work.
Main code
public class originator implements cloneable {private originator orignator;private string state = "";p ublic string Getstat E () {return this.state;} public void SetState (String state) {this.state = state;} public void Creatememento () {orignator = This.clone ();} public void Backstate () {this.setstate (Orignator.getstate ());} @Overrideprotected Originator Clone () {try {return (originator) Super.clone ()} catch (Clonenotsupportedexception e) { E.printstacktrace ();} return null;}}
Test class
public class Maintest {public static void main (string[] args) {Originator orig = new originator (); Orig.setstate ("very good"); System.out.println (Orig.getstate ()); Orig.creatememento (); Orig.setstate ("bad"); System.out.println (Orig.getstate ()); Orig.backstate (); System.out.println (Orig.getstate ());}}
case Three: Multiple parameters in the case of Memo mode
In this example, through the reflection technique, we get the parameter information, call its set and get method to set and get the value of the parameter in the object, and encapsulate the object's property value into HashMap, and when the backup is read its value by GET, it is stored in HashMap. The value in the stored HashMap is set to the original property by the set method when the rollback is rolled back.
In addition, the memo administrator role implements a supported version of the memo recovery, which can be restored according to the specific needs, i.e., allowing recovery to a previous version.
Main code
Memo Administrator Role Class
public class Caretaker {private map<string, memento> mementomap = new hashmap<string, memento> ();p ublic MAP&L T String, memento> Getmementomap () {return mementomap;} public void Setmementomap (map<string, memento> mementomap) {this.mementomap = Mementomap;}}
Memo Role Class
public class Memento {private hashmap<string, object> statemap;public Memento (hashmap<string, object> Statemap) {this.statemap = Statemap;} Public hashmap<string, Object> Getstatemap () {return statemap;} public void Setstatemap (hashmap<string, object> statemap) {this.statemap = Statemap;}}
Initiator Role Class
public class Originator {private String state1 = "";p rivate string state2 = "";p rivate string state3 = "";p ublic string ge TState1 () {return state1;} public void SetState1 (String state1) {this.state1 = state1;} Public String GetState2 () {return state2;} public void SetState2 (String state2) {this.state2 = State2;} Public String GetState3 () {return state3;} public void SetState3 (String state3) {this.state3 = State3;} Public Memento Creatememento () {Return to New Memento (Beanutils.store (This));} public void Backstate (Memento Memento) {Beanutils.back (this, Memento.getstatemap ());} @Overridepublic String toString () {return "originator [state1=" + State1 + ", state2=" + state2+ ", state3=" + State3 + "] ";}}
Class attributes are worth storing in recovery helper classes
public class Beanutils {public static hashmap<string, object> store (Object obj) {hashmap<string, object> stat EMap = new hashmap<string, object> (); try {BeanInfo BeanInfo = Introspector.getbeaninfo (Obj.getclass ()); Propertydescriptor[] Properties = beaninfo.getpropertydescriptors (); for (PropertyDescriptor proper:properties) { String propertyname = Proper.getname (); Method Readmethod = Proper.getreadmethod (), Object filedvalue = Readmethod.invoke (obj, new object[]{}); Propertyname.equalsignorecase ("class")) Statemap.put (PropertyName, Filedvalue);}} catch (Exception e) {e.printstacktrace ();} return statemap;} public static void Back (Object obj, hashmap<string, object> statemap) {try {BeanInfo BeanInfo = Introspector.getbean Info (Obj.getclass ()); propertydescriptor[] Propers = Beaninfo.getpropertydescriptors (); for (PropertyDescriptor proper:propers) {String Propername = Proper.getname (); method = Proper.getwritemethod (); if (Statemap.containskey (Propername)) {MethoD.invoke (obj, Statemap.get (propername));}}} catch (Exception e) {e.printstacktrace ();}}}
Test class
public class Maintest {public static void main (string[] args) {Originator orig = new originator (); Orig.setstate1 ("very good"); Ori G.setstate2 ("very bad"); Orig.setstate3 ("very good"); hashmap<string, memento> mementomap = new hashmap<string, memento> (); Caretaker taker = new Caretaker () Mementomap.put ("001", Orig.creatememento ()); Orig.setstate1 ("very good"); Orig.setstate2 (" Very good "); Orig.setstate3 (" very good "); Mementomap.put (" 002 ", Orig.creatememento ()); Taker.setmementomap (mementomap); o Rig.backstate (Taker.getmementomap (). Get ("001")); System.out.println (orig); Orig.backstate (Taker.getmementomap (). Get ("002")); System.out.println (orig);}}
Memo Mode Benefits
1, to provide users with a mechanism to restore the state. Can be a user can easily return to a history of the state.
2, the realization of the encapsulation of information. So that users do not need to care about the state of the preservation details.
Memo Mode Disadvantage
Consume resources. If the class has too many member variables, it is bound to occupy a larger resource, and each time the save consumes a certain amount of memory.
Memo Mode application Scenario
1, need to save an object at a certain point in the State or part of the state.
2, if you use an interface to let other objects get these states, will expose the object's implementation details and destroy the encapsulation of the object, an object does not want the outside world directly access its internal state, through the owner can indirectly access its internal state.
design mode code download
Design Pattern Source Code
"Onlookers" design mode (26)--Behavioral Memo mode (Memento pattern)