Main contents of this paper:
- Introducing the Memo Mode
- The sample
- Summary of Memo Model
For more information, please visit my personal blog: Laijianfeng.org
Memo mode
The memo pattern is often encountered, such as the following scenarios:
-
Browser rollback: The browser usually has browsing history. When we click a link on a web page several times, we can click the left arrow in the upper left corner to go back to the previous page, and then click the right arrow to go back to the current page
-
Database backup and restore: Common databases support backup and restore operations. Backup means reserving existing data or records, and restore means restoring retained data to corresponding tables
-
Editor undo and redo: Edit the text in the editor. Press Ctrl + Z to undo the wrong text, and press Ctrl + Y to redo the wrong text
-
Vm Snapshot generation and restoration: A snapshot can be generated for a VM. If an error occurs on the VM, the VM can be restored to the snapshot appearance
-
Git version management: Git is the most common version management software, in fact, every new version of Git will automatically string them into a timeline, each version has a version number, using Git reset –hard version number to go back to the specified version, so that the code time travel back to a historical point in the past
-
Chess: In chess, sometimes a quick move can be reversed, back to the previous move and play again
Memento Pattern: Capture the internal state of an object and save the state outside of the object without breaking encapsulation, so that the object can be restored to its original saved state at a later time. It is an object behavior pattern, alias Token.
role
Originator: A common class that can create a memo and store its current internal state, or that can be used to restore its internal state. The Originator is usually a class that needs to store its internal state.
Memento: Stores the internal state of the originator, which internal state is saved according to the originator. In general, the design of memo can refer to the design of the originator and determine the attributes in the memo class according to the actual needs. Note that the memo object cannot be used directly by any class other than the originator itself and the responsible human, and the implementation mechanism of the originator design varies between programming languages.
Caretaker: The Caretaker keeps the memo but does not act on it or check the contents. In charge of the human can store one or more memo objects, it is only responsible for storing objects, and cannot modify objects, also do not need to know the implementation details of the object.
The core of the memo pattern is the memo class and the design of the responsible human for managing the memo.
The sample
Chess examples, you can play chess, repent chess, undo repent chess and so on
Chessman, the original player
@Data
@AllArgsConstructor
class Chessman {
private String label;
private int x;
private int y;
// Save the state
public ChessmanMemento save(a) {
return new ChessmanMemento(this.label, this.x, this.y);
}
// Restore the status
public void restore(ChessmanMemento memento) {
this.label = memento.getLabel();
this.x = memento.getX();
this.y = memento.getY();
}
public void show(a) {
System.out.println(String.format("Pawn <%s> : current position is: <%d, %d>".this.getLabel(), this.getX(), this.getY())); }}Copy the code
The memento character ChessmanMemento
@Data
@AllArgsConstructor
class ChessmanMemento {
private String label;
private int x;
private int y;
}
Copy the code
Responsible role MementoCaretaker
class MementoCaretaker {
// Define a collection to store memos
private ArrayList mementolist = new ArrayList();
public ChessmanMemento getMemento(int i) {
return (ChessmanMemento) mementolist.get(i);
}
public void addMemento(ChessmanMemento memento) { mementolist.add(memento); }}Copy the code
The chess client maintains a MementoCaretaker object
class Client {
private static int index = -1;
private static MementoCaretaker mc = new MementoCaretaker();
public static void main(String args[]) {
Chessman chess = new Chessman("Car".1.1);
play(chess);
chess.setY(4);
play(chess);
chess.setX(5);
play(chess);
undo(chess, index);
undo(chess, index);
redo(chess, index);
redo(chess, index);
}
// Play chess while saving the memo
public static void play(Chessman chess) {
mc.addMemento(chess.save());
index++;
chess.show();
}
// Undo the last memo
public static void undo(Chessman chess, int i) {
System.out.println(Back "* * * * * * * * * * * *");
index--;
chess.restore(mc.getMemento(i - 1));
chess.show();
}
// Undo the contrition and proceed to the next memo
public static void redo(Chessman chess, int i) {
System.out.println("****** undo regret ******");
index++;
chess.restore(mc.getMemento(i + 1)); chess.show(); }}Copy the code
The output is as follows, repentance succeeds, undo repentance succeeds
Pieces < > car: the current position is: 1 > < 1, the pawn < > car: the current position is: > < 1, 4 pieces < > car: the current position is: < 5, 4 > back * * * * * * * * * * * * pieces < > car: the current position is: < 1, 4 > back * * * * * * * * * * * * pieces < > car: the current position is: 1 > < 1, * * * * * * undo back * * * * * * pieces < > car: the current position is: < 1, 4 > * * * * * * undo back * * * * * * pieces < > car: the current position is: < 5, 4 >Copy the code
The class diagram below
Summary of Memo Model
The main advantages of the memo mode are as follows:
-
It provides an implementation mechanism for state recovery that allows users to easily go back to a specific historical step, and when a new state is invalid or problematic, the state can be restored using a temporarily stored memo.
-
Memos encapsulate information. A memos object is a representation of the state of the original object and cannot be changed by other code. Memos store the state of the originator. Using lists, stacks and other collections to store memos can achieve multiple undo operations.
The main disadvantages of the memo mode are as follows:
- If too many member variables of the original developer class need to be saved, it will inevitably occupy a large amount of storage space. Every time the state of the object is saved, certain system resources will be consumed.
Applicable scenarios:
-
Saves all or part of the state of an object at a point in time, so that it can be restored to its previous state later when needed to undo it.
-
Prevent external objects from destroying the encapsulation of an object’s historical state and from exposing the implementation details of the object’s historical state to external objects.
Due to the JDK, Spring, there are very few in the Mybatis memo mode, maybe Spring webflow StateManageableMessageContext interface of a, but is really very rare, so there is no analysis of the typical application source code
Recommended reading
Design patterns and typical application of design patterns | | simple factory pattern factory method pattern and a typical application of design patterns | the abstract factory pattern and a typical application of design pattern model and typical application design | | builders prototype model and typical application design model and typical application design | | appearance Decorator pattern and a typical application of design pattern model and typical application design | | adapter the flyweight pattern and a typical application of design patterns | | portfolio model and typical application design pattern template method pattern and a typical application of design patterns | the iterator pattern and a typical application of design patterns | strategy pattern and typical applications Design patterns | observer pattern and typical applications