23 design modes (15): Memorandum Mode

Source: Internet
Author: User

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 necessary, 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 a single-State single-Backup example. The logic is easy: The state variable in the originator class needs to be backed up for recovery when necessary; in the memento class, there is also a state variable 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 only has one state variable to be backed up. In general, the initiator role is generally 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, which is a multi-state multi-Backup memorandum. There are many ways to implement memos. There are many variants and processing methods in the memorandum mode, which are generally not used in a way like General Code. In most cases, the memorandum Mode, is multi-state and multi-Backup. In fact, implementing multi-state and multi-backup is also very easy. The most common method is to add a map container in memento to store all the states, in the caretaker class, a map container is used to store all the backups. The following is an example of multi-state and multi-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 ("=== changed 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 model 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 must be stored in the memorandum object, which consumes more resources than the worker.

If you need to provide rollback operations, the memorandum mode is suitable, such as JDBC transaction operations and text editor Ctrl + z recovery.

23 design modes (15): Memorandum Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.