“This is the 31st day of my participation in the August Genwen Challenge.More challenges in August”
First, what is the memo mode?
The Memento Pattern, also known as the Snapshot Pattern or Token Pattern, captures the internal state of an object and saves the state outside the object without breaking the encapsulation. This allows the object to be restored later to its original saved state, in behavioral mode.
In software systems, the memo model can provide us with a “regret medicine” mechanism by storing snapshots of the system’s various historical states, so that we can roll back the system to a certain historical state at any time.
2. Application scenarios of memo mode
For us programmers, we may use the memo mode every day. For example, Git and SVN we use every day can provide a function of code version withdrawal. Another realistic scenario would be the game’s archiving feature, which stores the game’s current progress to a local file system or database so that the next time the game is played, the player can pick up where he left off.
The memo mode applies to the following application scenarios
- Scenarios in which historical snapshots need to be saved.
- You want to store state outside of an object that no other class object can access.
Roles involved in the memo mode
Let’s start with the general UML class diagram for the Memo pattern:
From the UML class diagram, we can see that the memo pattern consists of three main roles:
- Originator: Creates a memo to record the status to be saved and has the status rollback function.
- Memento: Used to store the internal state of the Originator and prevent objects other than the Originator from accessing it.
- Caretaker role: Stores the Caretaker and provides management Memento, and the Caretaker cannot be used or accessed.
Fourth, the use of stack management landing memo mode
We’ve all used rich text editors on web pages, often with drafts, undo, and so on. Let’s use a piece of code to implement one of these functions. Let’s say we publish an article, and the editing process takes a long time, with lots of retractions and revisions. It might even take days to write a good article, so you might save your edited content to the draft box in real time.
First create the initiator role Editor class:
@Data
@AllArgsConstructor
public class Editor {
private String title;
private String content;
private String images;
public ArticleMemento saveToMemento(a) {
ArticleMemento articleMemento = new ArticleMemento(this.title, this.content, this.images);
return articleMemento;
}
public void undoFromMemento(ArticleMemento articleMemento) {
this.title = articleMemento.getTitle();
this.content = articleMemento.getContent();
this.images = articleMemento.getImages(); }}Copy the code
Then create the memento role ArticleMemento class:
@Data
@AllArgsConstructor
public class ArticleMemento {
private String title;
private String content;
private String images;
}
Copy the code
Finally create the DraftsBox class for the memo Management role:
public class DraftsBox {
private final Stack<ArticleMemento> stack = new Stack<>();
public ArticleMemento getMemento(a) {
ArticleMemento articleMemento = stack.pop();
return articleMemento;
}
public void addMemento(ArticleMemento articleMemento) { stack.push(articleMemento); }}Copy the code
Write client-side test code:
public class Test {
public static void main(String[] args) {
DraftsBox draftsBox = new DraftsBox();
Editor editor = new Editor("Design Pattern Learning Journey"."Study hard and make progress every day."."666888.png");
ArticleMemento articleMemento = editor.saveToMemento();
draftsBox.addMemento(articleMemento);
System.out.println("Title:" + editor.getTitle() + "\n" + Content:" + editor.getContent() + "\n" + "Illustration:" + editor.getImages());
System.out.println("Completed message:" + editor);
System.out.println("==== First revised article ====");
editor.setTitle("Memo Mode");
editor.setContent("Mark is learning this mode...");
System.out.println("==== First modified article completed ====");
System.out.println("Completed message:" + editor);
articleMemento = editor.saveToMemento();
draftsBox.addMemento(articleMemento);
System.out.println("==== Save to draft box ====");
System.out.println("==== Second revision of the article ====");
editor.setTitle("Singleton pattern");
editor.setContent("Zoe is learning this mode...");
System.out.println("Completed message:" + editor);
System.out.println("==== Second revision completed ====");
System.out.println("==== revoke ==== for the first time");
articleMemento = draftsBox.getMemento();
editor.undoFromMemento(articleMemento);
System.out.println("Completed message:" + editor);
System.out.println("==== First undo complete ====");
System.out.println("==== withdrawn for the second time ====");
articleMemento = draftsBox.getMemento();
editor.undoFromMemento(articleMemento);
System.out.println("Completed message:" + editor);
System.out.println("==== Second cancellation completed ===="); }}Copy the code
The running results are as follows:
Five, the advantages and disadvantages of the memo mode
Advantages:
- It simplifies the responsibility of Originator classes, isolates state storage and retrieval, and encapsulates information without requiring clients to care about details of state storage.
- Provides the status rollback function.
Disadvantages:
- Consuming resources: If there are too many states to save, each save consumes a lot of memory.
Six, friendship links
Design Patterns – Factory Patterns learning tour
Design Patterns – a learning tour of singleton patterns
Design Patterns – A learning journey of prototyping patterns
Design Patterns – Builder patterns learning tour
Design Patterns – Agent patterns learning tour
Design Patterns – A learning tour of facade patterns
Design Patterns – A learning tour of decorator patterns
Design Patterns – Enjoy yuan patterns learning journey
Design Patterns – A learning journey of composite patterns
Design Patterns – Adapter patterns learning journey
Design Patterns – Bridge patterns learning journey
Design Patterns – Delegation patterns learning journey
Design Patterns – a template method pattern learning journey
Design Patterns – A learning journey of Strategic patterns
Design Patterns – A learning journey of chain of Responsibility patterns
Design Patterns – An iterator pattern learning journey
Design Mode – Command mode learning tour
Design pattern-state pattern learning journey
Welcome to follow the wechat public account (MarkZoe) to learn from and communicate with each other.