Easy to master Java Memo mode _java

Source: Internet
Author: User

definition: saves a state of an object so that it can be recovered at the appropriate time

Characteristics:

1, to provide users with a mechanism to restore the state, you can make it easier for users to return to a certain historical state.

2, the implementation of information encapsulation, so that users do not need to care about the state of the Save details.

Enterprise-class applications and applications in common frameworks: a common text editor uses this pattern

Instance:

Note: There is only a undo operation in this instance, there is no forward restore operation

/** * Target object: Object to be remembered/class Word {private String content;
 Private String image;
 Private String table;
 Public Word (string content, string image, String table) {super ();
 this.content = content;
 This.image = image;
 this.table = table;
 Public Wordmemento Memento () {Return to New Wordmemento (this);
 public void Recovery (Wordmemento memento) {this.content = Memento.getcontent ();
 This.image = Memento.getimage ();
 this.table = Memento.gettable ();
 Public String getcontent () {return content;
 public void SetContent (String content) {this.content = content;
 Public String GetImage () {return image;
 public void SetImage (String image) {this.image = image;
 Public String gettable () {return table;
 public void settable (String table) {this.table = table;
 }/** * Memo object/class wordmemento{private String content;
 Private String image;
 
 Private String table; Public Wordmemento (Word word) {this.content = Word.getcontent ();
 This.image = Word.getimage ();
 this.table = Word.gettable ();
 Public String getcontent () {return content;
 public void SetContent (String content) {this.content = content;
 Public String GetImage () {return image;
 public void SetImage (String image) {this.image = image;
 Public String gettable () {return table;
 public void settable (String table) {this.table = table;
 }/** * Owner object: Responsible for record Memo object/class caretaker{private list<wordmemento> List = new arraylist<> ();
 
 private int index = 0;
 public void Setmemento (Wordmemento memento) {List.add (memento);
 This.index = List.size ();
  Public Wordmemento Getwordmemento () {if (index = = 0) {System.out.println ("No content to restore");
 return null;
 } Wordmemento memento = List.get (index-1);
 List.remove (index-1);
 index--;
 return memento; }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.