1. Definition
Without destroying encapsulation, capture the internal state of an object and save this state outside the object. In this way, the object can be restored to its original saved state later.
2. Reasons for use
Want to restore the original state of the object at some point.
3. Examples of applicable situations
There are many applications of the memo mode, but we have seen it, but we haven't thought about it carefully. This is just the use of the memo mode. Let me give you a few examples:
eg1. Use of memos in jsp+javabean:
When adding an account in a system, you need to fill in the user name, password, contact number, address and other information in the form. If some fields are not filled in or are incorrectly filled, when the user clicks the "Submit" button, you need to save the options entered by the user on the new page and prompt for the wrong options. This is achieved using the scope="request" or scope="session" characteristics of JavaBean, that is, it is achieved using the memo mode.
eg2. When repairing the brakes of the car. First remove the baffles on both sides to expose the left and right brake pads. Only one piece can be removed, and the other piece serves as a memo to indicate how the brake is installed. Only after the repair of this piece is completed can another piece be removed. When the second piece is removed, the first piece becomes a memo.
eg3. It is said that there is no regret medicine to buy in life. We are all paying the price for what we do, but in the soft world, there are "regret medicine". After I change certain states of something, as long as we save certain states of the thing before, we can restore the state of the thing through the memo mode. In fact, this is also a "moonlight treasure box" that can turn back time, and the word "magic" is amazing.
4. Class diagram structure and description
(1) The class diagram is as follows:
(2) Class Description (i) Memento: Memorandum Role, the main work is as follows:
Store the internal state of the initiator object;
It can protect its content from being read by any object other than the Originator object.
(ii) Originator: The role of the sponsor, mainly completes the following tasks:
Create a memo object with the current internal state;
Use the memo object to store its internal state.
(iii) Caretaker: The person in charge, the work is completed as follows:
Responsible for preserving memorandum objects;
The contents of the memo object are not saved.
5. Example
/** * Data object*/ public class DataState { private String action; public void setAction(String action) { this.action = action; } public String getAction() { return action; } } /** * An object that saves the copy of the internal state of another object. In this way, the object can be restored to the original saved state in the future. */ import java.io.File; import java.io.Serializable; public class Memento implements Serializable { /*private int number; private File file = null; public Memento(Originator o) { this.number = o.getNumber(); this.file = o.getFile(); } public int getNumber() { return this.number; } public void setNumber(int number) { this.number = number; } public File getFile() { return this.file; } public void setFile(File file) { this.file = file; } */ private DataState state; public Memento(Originator o) { this.state = o.getState(); } public DataState getState() { return state; } public void setState(DataState state) { this.state = state; } } public class Originator { /* private int number; private File file = null; public Originator() { } // Create a Memento and pass it into public Memento as a parameter public Memento getMemento() { return new Memento(this); } // Take out the saved data from Memento and restore it to its original state public void setMemento(Memento m) { number = m.getNumber(); file = m.getFile(); } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public File getFile() { return file; } public void setFile(File file) { this.file = file; }*/ private DataState state; public Originator() { } public Originator(DataState state) { this.state = state; } // Create a Memento and pass it into public Memento as a parameter public Memento getMemento() { return new Memento(this); } // Take out the saved data from Memento and restore it to its original state public void setMemento(Memento m) { /* * The object created by getMemento() is saved in a container. * When it needs to be restored, pass it into the current method, and then use getState() to obtain */ this.state = m.getState(); } public DataState getState() { return state; } public void setState(DataState state) { this.state = state; } } /* * Originator is used to load data, create Memento objects, and restore the original data through Memento*/ public class Test { public static void main(String[] args) { // Originator originator = new Originator(); // originator.setNumber(8); // // Memento memento = originator.getMemento(); // System.out.println(memento.getNumber()); DataState state = new DataState(); state.setAction("copy a character"); Originator originator = new Originator(); System.out.println("Create original data"); originator.setState(state); System.out.println("Create memorandum object, save original data state"); Memento memento = originator.getMemento(); System.out.println("Create a new data"); originator.setState(new DataState()); System.out.println("After creating new data:" + originator.getState().getAction()); /* * memento needs to be saved in a certain place and retrieved when needed to restore the data saved inside it*/ System.out.println("Restore the original data after creating new data"); originator.setMemento(memento); System.out.println(originator.getState().getAction()); } }Print:
Create original data creation memorandum object, save the original data state Create a new data After creating new data: null After creating new data, restore the original data copy a character