This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Welcome to today’s class. Today we’re going to look at a less commonly used pattern —– memo pattern. This month, I will talk about Java design patterns in detail. Please click on the profile picture and follow my column. I will keep updating.
Series of articles:
Singletons of design patterns
The factory model of design pattern
The builder pattern of design patterns
Proxy patterns of design patterns
The visitor pattern of design patterns
Adaptor patterns for design patterns
The designer mode of the design mode
Java state pattern | monitoring state change anytime and anywhere
Java observer pattern | how to notice things change
. Under continuous update
Without further ado, let’s get to the point
Memorandum Mode
Compared to the other modes, the memo mode is less commonly used. I think it will be easy to use and less abstract to understand, so this section should be the simpler one.
The official definition is to capture and externalize the internal state of an object so that it can be recovered later.
It’s a copy of a database. It’s a copy of a database. It’s a copy of a database. Although this memo is ostensibly called a memo, it is more commonly understood as “backup mode.”
Let’s look at a simple class diagram
From this diagram, we can see that the memo pattern contains two key actors.
-
Originator: In addition to creating its own properties and business logic, it provides methods create() and restore(memento) to save and restore a copy of the object.
-
Memento: To hold the state of all the properties of the original object so that it can be undone in the future.
Let’s first look at the corresponding code implementation in the figure above. , and then combined with the business to achieve. First, we create the Originator object, which has four properties: State to show the current state of the object, name to simulate the business property
public class Originator {
private String state = "Original object"; // Print the current status
private String name;
public Originator(a) {}// Omit the get and set methods
// Backup (memo)
public Memento create(a) {
return new Memento(name);
}
// Undo return
public void restore(Memento m) {
this.state = m.getState();
this.name = m.getName(); }}Copy the code
Then, when you create the Memento, you’ll notice that the Memento is almost identical to the properties of the original object.
public class Memento {
private String state = "Restore from backup object to original object"; // Print the current status
private String name;
public Memento(String name) {
this.name = name;
}
// Omit the get and set methods
}
Copy the code
public class Demo {
public static void main(String[] args) {
Originator originator = new Originator();
originator.setName("Zhang");
System.out.println(originator);
Memento memento = originator.create();
originator.setName("Bill"); System.out.println(originator); originator.restore(memento); System.out.println(originator); }}Copy the code
Let’s look at a scenario implementation
Scenario and code implementation
For example, we are writing about gold. Suddenly my computer crashed. Direct restart, that I this very not easy to write a good article how to do, do you still need to write again. Of course not
I can start by creating a Blog object that contains the nugget ID, title, and content, which represent the unique NUGget ID, title, and content of the Blog. We also provide the createMemento() and restore(BlogMemento m) methods for creating memos, which can be used to create memos and restore the original Blog object through memos.
Code implementation
// Blog content entity
public class Blog {
private long id;
private String title;
private String content;
public Blog(long id, String title) {
super(a);this.id = id;
this.title = title;
}
/ / get the set omitted
public BlogMemento createMemento(a) {
BlogMemento m = new BlogMemento(id, title, content);
return m;
}
public void restore(BlogMemento m) {
this.id = m.getId();
this.title = m.getTitle();
this.content = m.getContent(); }}Copy the code
Then, you create a Blog memo object, BlogMemento, which also copies all the attributes you need for your Blog.
public class BlogMemento {
private final long id;
private final String title;
private final String content;
public BlogMemento(long id, String title, String content) {
super(a);this.id = id;
this.title = title;
this.content = content;
}
public long getId(a) {
return id;
}
public String getTitle(a) {
return title;
}
public String getContent(a) {
returncontent; }}Copy the code
This creates a memo based on the Blog object. Finally, let’s run a unit test to see what happens.
public class Client {
public static void main(String[] args) {
Blog blog = new Blog(1."Java memo mode | how to record history information");
blog.setContent("A record of historical information........"); // The original article content
System.out.println(blog);
BlogMemento memento = blog.createMemento(); // Create a blog memo
blog.setContent("Modify picture"); // Change the content
System.out.println(blog);
blog.restore(memento); // Undo the operation
System.out.println(blog); // The original content is displayed}}Copy the code
As you can see from the code, the memo pattern works well in frequent undo and restore scenarios
conclusion
Memo mode is also called snapshot mode, specifically, by capturing the object state at a certain point in time, and then save it to an external object, so that the object can be restored to the specified point in time when needed.
Note: When using the memo mode, it is important not to mistake it for a universal backup mode, but to properly evaluate the memory space used by the object before deciding whether to use the memo mode.
Well, today’s story is over, the memorandum mode is relatively simple, not much to repeat.
overtones
Thank you for reading, and if you feel you’ve learned something, please like it and follow it.
I have included this chapter in my project, click on the project below, and follow the column. I will publish dry items every day, and I will continue to enter design patterns this month.
Come on! See you next time!