java-design mode (behavioral)-"Memo mode"

Source: Internet
Author: User

http://alaric.iteye.com/blog/1931253

Http://www.cnblogs.com/java-my-life/archive/2012/06/06/2534942.html

1. Memo Mode (Memento)

Definition: The main purpose is to save a state of an object in order to restore the object at the appropriate time, and the individual feels that the backup mode is more image.

In layman's words: Assuming there are various attributes in the original class A,a, a can determine the attributes that need to be backed up, and Memo Class B is used to store

A some of the internal state, Class C, is a used to store the memo, and can only be stored, can not be modified and other operations

Diagram:

2. For example

2.1 Original class: Primitive class:

Original classes public class Original {//state information private String state= "";    Memo: Save status information Public Memento Creatememento () {return new Memento (state);} Public String GetState () {return state;}    Reset status information: Resets the public void ResetState (Memento Memento) {   System.out.print ("Reset-") from the memo, SetState (Memento.getstate ()) ;} public void SetState (String state) {this.state = state; System.out.println ("Set-state:" +state);}} Memo: Non-modifiable status information class Memento{private string State=null;public string GetState () {return state;} Public Memento (String state) {this.state=state;}}

2.2 Storage classes:

Save the memo storage class public class Storage {private Memento memento=null;public Storage (Memento Memento) {This.memento=memento;} Public Memento Getmemento () {return Memento;}}

2.3 Test:

public class Test {public static void main (string[] args) {//TODO auto-generated method stub        Original ori=new Origina L ();        Ori.setstate ("Off");        Save Memo        Storage  sto=new Storage (Ori.creatememento ());        Change        the status Ori.setstate ("on");        Reset Status        ori.resetstate (Sto.getmemento ());}}

2.4 Operation Result:

Set-Status: Off Setting-Status: On reset-Settings-Status: Off

3. Black-Box operation (the memo is defined in the form of a privatized internal class and then accessed through an interface):

Original classes public class Original {//state information private String state= "";    Memo: Save information in the form of an internal class, inherit the interface, make it easy for outsiders to access it private class Memento implements Mementoif{public Memento (Original ori) {state= Ori.state;} Public String getState () {//TODO auto-generated method Stubreturn null;}} Public Memento Creatememento () {Return to New Memento (this);} Public String GetState () {return state;}    Reset status information: Resets the public void ResetState (mementoif  Memento) {   System.out.print ("Reset-") from the memo; Memento mem= (Memento) Memento; SetState (Mem.getstate ());} public void SetState (String state) {this.state = state; System.out.println ("Set-state:" +state);}} Create an interface: facilitates external access to the inner class of the memo interface Mementoif {}

Test:

public class Test {public static void main (string[] args) {//TODO auto-generated method stub        Original ori=new Origina L ();        Ori.setstate ("Off");        Save Memo        Mementoif Memento=ori.creatememento ();        Change        the status Ori.setstate ("on");        Reset Status        ori.resetstate (memento);}        }

Results:

Set-Status: Off Setting-Status: On reset-Settings-Status: Off

  

java-design mode (behavioral)-"Memo mode"

Related Article

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.