Memento (Memo Mechanism)

zhaozj2021-02-16  53

MEMENTO Definition: MEMENTO is an object that holds a copy of the other object. This will be restored to the originally saved state in this future.

MEMENTO mode is relatively good, let's see the following code:

public class Originator {private int number; private File file = null; public Originator () {} // Create a Memento public Memento getMemento () {return new Memento (this);} // return to the original value public void setMemento (Memento m) {Number = m.Number; file = m.file;}}

Let's take a look at the MEMENTO category:

Private class meteo imports java.io.serializable {private int number; private file file = null; public memento (Originator O) {Number = O.Number; File = O.File;}}

It can be seen that the value of Number and File in the Originator can be seen in Memento. By calling the Number and File values ​​in the originator, you can recover by calling the setMemento () method.

The disadvantage of the MEMENTO mode is that it costs, and if there are many internal status, then save one, there is no intention to waste a lot of memory.

MEMENTO mode In JSP JavaBean, in JSP applications, we usually have a lot of forms that require user input, such as user registration, you need to enter your name and email, if some item users have not filled or fill in errors, we hope to press in the user After "Submit Submit", through the JSP program, it is found that there is indeed unfilled, and the red word is displayed under this item, and the user has just entered the entered entry.

The first name is the user has entered, and the Last Name is not input, and we prompt the red word warning.

The implementation of this technology is to use the JavaBean scope = "request" or scope = "session" feature, which is MEMENTO mode.

Specific examples and code See JavaWorld's English original, the JavaBean form input feature See my other article.

转载请注明原文地址:https://www.9cbs.com/read-20605.html

New Post(0)