Java design mode Memo pattern "Memento pattern"

Source: Internet
Author: User

I. Overview

Capture the internal state of an object without destroying it, and save the state outside the object so that it can be restored to its previously saved state. It is an object-behavioral pattern .


Second, the application scenario

1, similar to the implementation of the Undo function, save an object at a certain time part of the state or all the state, when it is needed later can be restored to the previous state.

2, the object's historical state encapsulation, avoid the object's historical state of implementation details exposed to the outside world.


Third, UML class diagram


Iv. participants

1,originator(original): It is a normal class, you can create a memo and store its current internal state, you can also use the memo to restore its internal state, generally will need to save the internal state of the class design as the original generator.

2,Memento(Memo): Store the internal state of the original, according to the original generator to decide which internal state to save. The design of a memo can generally refer to the design of the original, and determine the attributes in the memo class according to the actual needs. It is important to note that, in addition to the sender itself and the responsible human, the memo object cannot be used directly by other classes.

3,caretaker(Memo manager or person in charge): Responsible for saving the memo, but not the contents of the memo to operate or check. In this class, you can store one or more Memo objects, which are only responsible for storing objects, not modifying objects, or knowing the implementation details of an object.


V. Use case Learning

1, Memo mode role of the original generator class: Originator.java

/** * Memo mode role of  the sender <br/> * contains the internal need to save the state attribute * @author [email protected] * */public class Originator {private String state;/** * Create a Memo object  * @return */public Memento Creatememento () {return new Memento (this);} /** * Restore the original predecessor status according to the Memo object   * @param o */public void Restorememento (Memento o) {this.state = O.getstate ();} Public String GetState () {return state;} public void SetState (String state) {this.state = state;}}

2. Memo Mode Role Memo class: Memento.java

/** * Memo Mode role  Memo <br/> * Store the internal status of the original generator * @author [email protected] * */public class Memento {private String state; Public Memento (originator originator) {this.state = Originator.getstate ();} Public String GetState () {return state;} public void SetState (String state) {this.state = state;}}

3, Memo mode role Memo manager or person in charge class: Caretaker.java

/** * Memo Mode role Memo manager or person in charge <br/> * is only responsible for storing memo objects, not modifying Memo objects, and not needing to know the implementation details of the memo object <br/> * <b> extension:<br/> * If you want to implement a multi-step undo memo pattern, you only need to use an array collection in this class to load the memo object for each step state, such as:<br/> *  //define an array collection to store multiple Memo objects  <br/>List< memento> mementos = new arraylist<memento> (); * </b> * @author [email protected] * */public class Caretaker {private Memento memento;public Memento Getmemento () { return memento;} public void Setmemento (Memento Memento) {this.memento = Memento;}}

4, the Client test class Client.java

public class Client {public static void main (string[] args) {Caretaker caretaker = new Caretaker (); originator originator = New originator ();//Initialize status ID "0" originator.setstate ("0");//Create Memo object with status "0" Memento memento_1 = Originator.creatememento ()///will record the originator status memo to caretaker memo manager to store Caretaker.setmemento (memento_1); showstate (originator); SYSTEM.OUT.PRINTLN ("-----Change the status of the original-----");//Change the status of the original generator is identified as "1" originator.setstate ("1"); showstate (originator); SYSTEM.OUT.PRINTLN ("-----is revoked to the previous state of the original-----"); Originator.restorememento (Caretaker.getmemento ()); Showstate ( originator);} private static void Showstate (originator originator) {SYSTEM.OUT.PRINTLN ("Current state of Originator:" + originator.getstate ());}}

5. Operation Result:

Current state of Originator: 0-----Change the status of the original-----originator Current state: 1-----Undo to the previous state of the original-----originator's current state: 0




Java design mode Memo pattern "Memento pattern"

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.