Definition:Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.
Type:Behavior
Class diagram:
During programming, we often need to save the intermediate state of the object. When needed, we can restore to this state. For example, when we use eclipse for programming, we want to return the status before the deletion if we make a mistake in writing (for example, accidentally deleting several lines of code by mistake, you can use Ctrl + Z to return data. In this case, we can use the memorandum mode.
Structure of memorandum Mode
- Initiator:Records the internal status of the current time point, defines the status of the backup scope, and creates and restores the memorandum data.
- Memorandum:Stores the internal status of the initiator object and provides the internal status required by the initiator when necessary.
- Manage Roles:Manage, save and provide memos.
General Code Implementation
Class originator {private string state = ""; Public String getstate () {return state;} public void setstate (string state) {This. state = State;} public memento creatememento () {return new memento (this. state);} public void restorememento (memento) {This. setstate (memento. getstate () ;}} class memento {private string state = ""; Public memento (string state) {This. state = State;} Public String getstate () {return state;} public void setstate (string state) {This. state = State ;}} class caretaker {private memento; Public memento getmemento () {return memento;} public void setmemento (memento) {This. memento = memento;} public class client {public static void main (string [] ARGs) {originator = new originator (); originator. setstate ("status 1"); system. out. println ("initial state:" + originator. getstate (); caretaker = new caretaker (); caretaker. setmemento (originator. creatememento (); originator. setstate ("state 2"); system. out. println ("changed status:" + originator. getstate (); originator. restoremento (caretaker. getmemento (); system. out. println ("Restore status:" + originator. getstate ());}}
The Code demonstrates an example of single-State and single-backup. The logic is very simple: the state variable in the originator class needs to be backed up for recovery as needed; The memento class also has a state variable, it is used to store the temporary state of the state variable in the originator class, and the caretaker class is used to manage the memorandum class, which is used to write the state to the memorandum object or retrieve the state.
Multi-state multi-Backup Memorandum
In the example of General Code demonstration, the originator class has only one state variable to be backed up. In general, the initiator role is usually a JavaBean, and there are more than one variable to be backed up in the object, there are more than one status to be backed up. This is a multi-state multi-Backup memorandum. There are many ways to implement the Memorandum. The memorandum mode has many deformation and processing methods, which are not used in general code. In most cases, the memorandum mode is multi-state and multi-Backup. In fact, it is easy to implement multi-state and multi-backup. The most common method is to add a map container in memento to store all States, in the caretaker class, a map container is used to store all backups. Here is an example of multi-state and multi-state backup:
Class originator {private string state1 = ""; private string state2 = ""; private string state3 = ""; Public String getstate1 () {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 = stat E3;} public memento creatememento () {return new memento (beanutils. backupprop (this);} public void restorememento (memento) {beanutils. restoreprop (this, memento. getstatemap ();} Public String tostring () {return "state1 =" + state1 + "state2 =" + state2 + "state3 =" + state3 ;}} class memento {private Map <string, Object> statemap; Public memento (Map <string, Object> map) {This. statemap = map;} public Map <string, obje CT> getstatemap () {return statemap;} public void setstatemap (Map <string, Object> statemap) {This. statemap = statemap;} class beanutils {public static Map <string, Object> backupprop (Object bean) {Map <string, Object> result = new hashmap <string, object> (); try {beaninfo = introspector. getbeaninfo (bean. getclass (); propertydescriptor [] descriptors = beaninfo. getpropertydescriptors (); For (propertyd Escriptor des: descriptors) {string fieldname = des. getname (); Method getter = des. getreadmethod (); object fieldvalue = getter. invoke (bean, new object [] {}); If (! Fieldname. equalsignorecase ("class") {result. put (fieldname, fieldvalue) ;}} catch (exception e) {e. printstacktrace ();} return result;} public static void restoreprop (Object bean, Map <string, Object> propmap) {try {beaninfo = introspector. getbeaninfo (bean. getclass (); propertydescriptor [] descriptors = beaninfo. getpropertydescriptors (); For (propertydescriptor des: descriptors) {string fieldname = des. getname (); If (propmap. containskey (fieldname) {method setter = des. getwritemethod (); setter. invoke (bean, new object [] {propmap. get (fieldname)}) ;}} catch (exception e) {e. printstacktrace () ;}} class caretaker {private Map <string, memento> memmap = new hashmap <string, memento> (); Public memento getmemento (string index) {return memmap. get (INDEX);} public void setmemento (string index, memento) {This. memmap. put (index, memento) ;}} class client {public static void main (string [] ARGs) {originator Ori = new originator (); caretaker = new caretaker (); ori. setstate1 ("China"); Ori. setstate2 ("strong"); Ori. setstate3 ("prosperous"); system. out. println ("=== initialization status ===\ N" + ORI); caretaker. setmemento ("001", Ori. creatememento (); Ori. setstate1 ("software"); Ori. setstate2 ("architecture"); Ori. setstate3 ("excellent"); system. out. println ("=== modified status ===\ N" + ORI); Ori. restoremento (caretaker. getmemento ("001"); system. out. println ("==== restored status ===\ N" + ORI );}}
Advantages and disadvantages of the memorandum model and applicable scenarios
The memorandum mode has the following advantages:
- When the status of the initiator role changes, it may be a wrong change. We can use the memo mode to restore the change.
- The backup status is stored outside the initiator role, so that the initiator role does not need to manage the status of each backup.
Disadvantages of the memorandum model:
- In practice, the memorandum mode is multi-state and multi-backup. The initiator role State needs to be stored in the memorandum object, causing serious resource consumption.
If you need to provide rollback operations, the memorandum mode is very suitable, such as JDBC transaction operations and text editor Ctrl + z recovery.
From: http://blog.csdn.net/zhengzhb/article/details/7697549
Definition:Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.
Type:Behavior
Class diagram:
During programming, we often need to save the intermediate state of the object. When needed, we can restore to this state. For example, when we use eclipse for programming, we want to return the status before the deletion if we make a mistake in writing (for example, accidentally deleting several lines of code by mistake, you can use Ctrl + Z to return data. In this case, we can use the memorandum mode.
Structure of memorandum Mode
- Initiator:Records the internal status of the current time point, defines the status of the backup scope, and creates and restores the memorandum data.
- Memorandum:Stores the internal status of the initiator object and provides the internal status required by the initiator when necessary.
- Manage Roles:Manage, save and provide memos.
General Code Implementation
Class originator {private string state = ""; Public String getstate () {return state;} public void setstate (string state) {This. state = State;} public memento creatememento () {return new memento (this. state);} public void restorememento (memento) {This. setstate (memento. getstate () ;}} class memento {private string state = ""; Public memento (string state) {This. state = State;} Public String getstate () {return state;} public void setstate (string state) {This. state = State ;}} class caretaker {private memento; Public memento getmemento () {return memento;} public void setmemento (memento) {This. memento = memento;} public class client {public static void main (string [] ARGs) {originator = new originator (); originator. setstate ("status 1"); system. out. println ("initial state:" + originator. getstate (); caretaker = new caretaker (); caretaker. setmemento (originator. creatememento (); originator. setstate ("state 2"); system. out. println ("changed status:" + originator. getstate (); originator. restoremento (caretaker. getmemento (); system. out. println ("Restore status:" + originator. getstate ());}}
The Code demonstrates an example of single-State and single-backup. The logic is very simple: the state variable in the originator class needs to be backed up for recovery as needed; The memento class also has a state variable, it is used to store the temporary state of the state variable in the originator class, and the caretaker class is used to manage the memorandum class, which is used to write the state to the memorandum object or retrieve the state.
Multi-state multi-Backup Memorandum
In the example of General Code demonstration, the originator class has only one state variable to be backed up. In general, the initiator role is usually a JavaBean, and there are more than one variable to be backed up in the object, there are more than one status to be backed up. This is a multi-state multi-Backup memorandum. There are many ways to implement the Memorandum. The memorandum mode has many deformation and processing methods, which are not used in general code. In most cases, the memorandum mode is multi-state and multi-Backup. In fact, it is easy to implement multi-state and multi-backup. The most common method is to add a map container in memento to store all States, in the caretaker class, a map container is used to store all backups. Here is an example of multi-state and multi-state backup:
Class originator {private string state1 = ""; private string state2 = ""; private string state3 = ""; Public String getstate1 () {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 = stat E3;} public memento creatememento () {return new memento (beanutils. backupprop (this);} public void restorememento (memento) {beanutils. restoreprop (this, memento. getstatemap ();} Public String tostring () {return "state1 =" + state1 + "state2 =" + state2 + "state3 =" + state3 ;}} class memento {private Map <string, Object> statemap; Public memento (Map <string, Object> map) {This. statemap = map;} public Map <string, obje CT> getstatemap () {return statemap;} public void setstatemap (Map <string, Object> statemap) {This. statemap = statemap;} class beanutils {public static Map <string, Object> backupprop (Object bean) {Map <string, Object> result = new hashmap <string, object> (); try {beaninfo = introspector. getbeaninfo (bean. getclass (); propertydescriptor [] descriptors = beaninfo. getpropertydescriptors (); For (propertyd Escriptor des: descriptors) {string fieldname = des. getname (); Method getter = des. getreadmethod (); object fieldvalue = getter. invoke (bean, new object [] {}); If (! Fieldname. equalsignorecase ("class") {result. put (fieldname, fieldvalue) ;}} catch (exception e) {e. printstacktrace ();} return result;} public static void restoreprop (Object bean, Map <string, Object> propmap) {try {beaninfo = introspector. getbeaninfo (bean. getclass (); propertydescriptor [] descriptors = beaninfo. getpropertydescriptors (); For (propertydescriptor des: descriptors) {string fieldname = des. getname (); If (propmap. containskey (fieldname) {method setter = des. getwritemethod (); setter. invoke (bean, new object [] {propmap. get (fieldname)}) ;}} catch (exception e) {e. printstacktrace () ;}} class caretaker {private Map <string, memento> memmap = new hashmap <string, memento> (); Public memento getmemento (string index) {return memmap. get (INDEX);} public void setmemento (string index, memento) {This. memmap. put (index, memento) ;}} class client {public static void main (string [] ARGs) {originator Ori = new originator (); caretaker = new caretaker (); ori. setstate1 ("China"); Ori. setstate2 ("strong"); Ori. setstate3 ("prosperous"); system. out. println ("=== initialization status ===\ N" + ORI); caretaker. setmemento ("001", Ori. creatememento (); Ori. setstate1 ("software"); Ori. setstate2 ("architecture"); Ori. setstate3 ("excellent"); system. out. println ("=== modified status ===\ N" + ORI); Ori. restoremento (caretaker. getmemento ("001"); system. out. println ("==== restored status ===\ N" + ORI );}}
Advantages and disadvantages of the memorandum model and applicable scenarios
The memorandum mode has the following advantages:
- When the status of the initiator role changes, it may be a wrong change. We can use the memo mode to restore the change.
- The backup status is stored outside the initiator role, so that the initiator role does not need to manage the status of each backup.
Disadvantages of the memorandum model:
- In practice, the memorandum mode is multi-state and multi-backup. The initiator role State needs to be stored in the memorandum object, causing serious resource consumption.
If you need to provide rollback operations, the memorandum mode is very suitable, such as JDBC transaction operations and text editor Ctrl + z recovery.
From: http://blog.csdn.net/zhengzhb/article/details/7697549