Definition and characteristics of pattern
Definition of memo mode: capture the internal state of an object without destroying the encapsulation, and save the state outside the object, so that the object can be restored to the original saved state when necessary in the future. This mode is also called snapshot mode.
Memo mode is an object behavior mode. Its main advantages are as follows.
- Provides a mechanism to restore state. When users need it, they can easily restore the data to a historical state.
- The encapsulation of internal state is realized. Except for the initiator who created it, no other object can access this state information.
- Simplified the human. The initiator does not need to manage and save each backup of its internal status. All status information is saved in the memo and managed by the manager, which is in line with the principle of single responsibility.
Its main disadvantage is: high resource consumption. If the internal state information to be saved is too much or too frequent, it will occupy a large memory resource.
Structure and implementation of pattern
The core of memo pattern is to design memo class and manager class for managing memo. Now let's learn its structure and implementation.
1. Structure of the model
The main roles of the memo model are as follows.
- Initiator role: it records the internal status information at the current time, provides the function of creating memos and recovering memo data, and realizes other business functions. It can access all the information in memos.
- Memo role: responsible for storing the internal status of the initiator and providing these internal status to the initiator when necessary.
- Caretaker role: manages memos and provides the function of saving and obtaining memos, but it cannot access and modify the contents of memos.
The structure diagram of memo mode is shown in Figure 1.
2. Implementation of mode
The implementation code of memo mode is as follows:
package net.biancheng.c.memento; public class MementoPattern { public static void main(String[] args) { Originator or = new Originator(); Caretaker cr = new Caretaker(); or.setState("S0"); System.out.println("Initial state:" + or.getState()); cr.setMemento(or.createMemento()); //Save status or.setState("S1"); System.out.println("New status:" + or.getState()); or.restoreMemento(cr.getMemento()); //Restore state System.out.println("Restore state:" + or.getState()); } } //memorandum class Memento { private String state; public Memento(String state) { this.state = state; } public void setState(String state) { this.state = state; } public String getState() { return state; } } //Initiator class Originator { private String state; public void setState(String state) { this.state = state; } public String getState() { return state; } public Memento createMemento() { return new Memento(state); } public void restoreMemento(Memento m) { this.setState(m.getState()); } } //controller class Caretaker { private Memento memento; public void setMemento(Memento m) { memento = m; } public Memento getMemento() { return memento; } }
Application scenario of pattern
After learning the definition, characteristics, structure and implementation of memo pattern, let's look at the following application scenarios of this pattern.
- Scenes that need to save and restore data, such as the archiving function of intermediate results when playing games.
- You need to provide a scenario that can roll back operations, such as Word, Notepad, Photoshop, Eclipse and other software, pressing Ctrl+Z during editing, and transaction operations in the database.
Mode extension
In the memo mode described earlier, there are examples of single state backup and multi state backup. The following describes how the memo mode is the same as Prototype mode Mixed use. In the memo mode, the information of the "initiator" is backed up by defining the "Memo", and the clone() method of the prototype mode has the self backup function. Therefore, if the initiator implements the clonable interface, it has the function of backing up itself. At this time, the memo class can be deleted, and its structure is shown in Figure 4.
The implementation code is as follows:
package net.biancheng.c.memento; public class PrototypeMemento { public static void main(String[] args) { OriginatorPrototype or = new OriginatorPrototype(); PrototypeCaretaker cr = new PrototypeCaretaker(); or.setState("S0"); System.out.println("Initial state:" + or.getState()); cr.setMemento(or.createMemento()); //Save status or.setState("S1"); System.out.println("New status:" + or.getState()); or.restoreMemento(cr.getMemento()); //Restore state System.out.println("Restore state:" + or.getState()); } } //Initiator prototype class OriginatorPrototype implements Cloneable { private String state; public void setState(String state) { this.state = state; } public String getState() { return state; } public OriginatorPrototype createMemento() { return this.clone(); } public void restoreMemento(OriginatorPrototype opt) { this.setState(opt.getState()); } public OriginatorPrototype clone() { try { return (OriginatorPrototype) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return null; } } //Prototype Manager class PrototypeCaretaker { private OriginatorPrototype opt; public void setMemento(OriginatorPrototype opt) { this.opt = opt; } public OriginatorPrototype getMemento() { return opt; } }
expand
Due to JDK Spring,Mybatis There are few memo modes in, so this Design pattern Do not do typical application source code analysis.
The DefaultMessageContext class in Spring Webflow implements the StateManageableMessageContext interface. Looking at its source code, you can find that its main logic is equivalent to backing up the Message. Interested partners can read and learn its source code.