Java Design Patterns Rookie series (19) model and implementation of Memo mode

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/40018967


Memo Mode (Memento): The primary purpose is to save a state of an object so that it can be restored at the appropriate time.

First, UML modeling:



Second, the Code implementation

/** * Memo Mode (MEMENTO): The main purpose is to save a state of an object so that the object can be restored at the appropriate time * Example: Original class--Create, Restore Memo */class Original {private String State;publi C Original (String state) {this.state = state;} Public String GetState () {return state;} public void SetState (String state) {this.state = state;} /** * Create Memo */public Memento Creatememento () {return new Memento (state); /** * Recovery Memo */public void Recovermemento (Memento Memento) {this.state = Memento.getstate ();}} /** * Memo */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;}}  /** * Used to store memos (holding memo instances): Can only be stored, cannot modify */class Storage {private Memento memento;public Storage (Memento Memento) {This.memento = Memento;} Public Memento Getmemento () {return Memento;}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {/** * Create original object */original Origina L = new Original ("daytime mode"); System.out.println ("Original initial state is:" + original.getsTate ());/** * Create notes * * Note: Original.creatememento () will pass the initial state (daylight mode) to the Memento object * * Storage.getmemento () can be called when needed To get the state (daylight mode) status * equals state (Daytime mode) This status has been delegated to the Storage object to save */storage Storage = new Storage (Original.creatememento ()) ; Original.setstate ("Night Mode"); System.out.println ("Original modified State is:" + original.getstate ());/** * Recovery Memo */original.recovermemento ( Storage.getmemento ()); System.out.println ("Original status after recovery:" + original.getstate ());}}

Iii. Summary

Memento Memo design mode is used to back up the current state of an object and, when needed, to restore the object's state at a certain point in time.


Java Design Patterns Rookie series (19) model and implementation of Memo 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.