Java design mode memo mode (Memento mode) Introduction _java

Source: Internet
Author: User

Memento definition: Memento is an object that holds a copy of the internal state of another object, so that the object can later be restored to its previously saved state.

Memento mode is relatively good understanding, we look at the following code:

Copy Code code as follows:

public class Originator {
private int number;
Private file File = null;
Public originator () {}
Create a memento
Public Memento Getmemento () {
Return to New Memento (this);
}
Revert to original value
public void Setmemento (Memento m) {
Number = M.number;
File = M.file;
}
}

Let's take another look at the Memento class:

Copy Code code as follows:

Private class Memento implements java.io.serializable{
private int number;
Private file File = null;
Public Memento (originator o) {
Number = O.number;
File = O.file;
}
}


The values of number and file in originator are saved in the visible memento. You can recover by calling the Setmemento () method by invoking the number and file values in originator.

The disadvantage of memento mode is that it is expensive, if the internal state of a lot, and then save a copy, no intention to waste a lot of memory.

The application of memento mode in Jsp+javabean

In JSP application, we usually have a lot of forms require user input, such as user registration, need to enter names and email, etc., if some of the table items users did not fill out or fill in the error, we hope that the user press "Submit", through the JSP program check, found that there are indeed unfinished projects The red Word displays a warning or error under the item, and also displays the table entry that the user has just entered.

In the following figure, the name is entered by the user, and the word "No" is entered, and we prompt the Red letter warning:

The realization of this technology is to use the JavaBean scope= "request" or scope= "session" characteristics, that is, the memento 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.