23 design patterns (18) java memorandum patterns, 23 Design Patterns
23 design patterns Article 18th: java memorandum patterns
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, defines the status of the backup range, 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.
Management 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 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 memento; public Memento getMemento () {return memento;} public void setMemento (Memento memento) {this. memento = memento;} public class Client {public static void main (String [] args) {Originator originator = new Originator (); originator. setState ("status 1"); System. out. println ("initial state:" + originator. getState (); Caretaker 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 = state3;} public Memento createMemento () {return new Memento (BeanUtils. backupProp (this);} public void restoreMemento (Memento 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. stat EMap = map;} public Map <String, Object> 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 beanInfo = Introspector. getBeanInfo (bean. getClass (); PropertyDescriptor [] descriptors = BeanInfo. getPropertyDescriptors (); for (PropertyDescriptor 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 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 memento) {this. memMap. put (index, memento) ;}} class Client {public static void main (String [] args) {Originator ori = new Originator (); Caretaker 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.
The memorandum model has the following Disadvantages:
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.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.