[Design Pattern Series 21] Memo Mode: I am so simple that it has been completely ignored

Design pattern series overview

Design Patterns Air ticket
Three factory models Boarding entrance
Strategy mode Boarding entrance
Delegation mode Boarding entrance
Template method pattern Boarding entrance
Observer mode Boarding entrance
Singleton mode Boarding entrance
Prototype mode Boarding entrance
Agency model Boarding entrance
Decorator mode Boarding entrance
Adapter mode Boarding entrance
Builder mode Boarding entrance
Chain of Responsibility Model Boarding entrance
Flyweight model Boarding entrance
Combination mode Boarding entrance
Facade pattern Boarding entrance
Bridge mode Boarding entrance
Intermediary model Boarding entrance
Iterator mode Boarding entrance
State mode Boarding entrance
Interpreter mode Boarding entrance
Memo mode Boarding entrance
Command mode Boarding entrance
Visitor mode Boarding entrance
Summary of 7 principles and design patterns of software design Boarding entrance

Preface

The memo pattern is a very simple design pattern, so simple that we have ignored the existence of this design pattern. The name of the memo mode may still be a bit unfamiliar to some people, but its another name, snapshot mode, may sound like a little understanding of what design mode this is. Next, let us look at the memo mode

What is the memo mode

Memento Pattern, also known as Snapshop Pattern or Token Pattern, means to capture the internal state of an object without destroying the encapsulation and save this state outside the object , So that we can restore the object to its original state when needed. The memo mode is a behavioral mode.

The memorandum model can provide us with a mechanism of "regret medicine". It stores a snapshot of each historical state in the system so that we can roll the system back to the historical state at any time.

Okay, the time to pretend is here again: Talk is cheap, Show you the code , let’s look at a very simple example first.

Example of memo mode

Let's take the common rich text editor's function of editing articles as an example to write a simple example to see how the memo mode is implemented.
1. First create a class to save the latest article information:

package com.zwx.design.pattern.memento;

import java.util.Date;

public class ArticleText {
    
    
    private String title;
    private String content;
    private Date createTime;

    public ArticleText(String title, String content, Date createTime) {
    
    
        this.title = title;
        this.content = content;
        this.createTime = createTime;
    }

    public String getTitle() {
    
    
        return title;
    }

    public void setTitle(String title) {
    
    
        this.title = title;
    }

    public String getContent() {
    
    
        return content;
    }

    public void setContent(String content) {
    
    
        this.content = content;
    }

    public Date getCreateTime() {
    
    
        return createTime;
    }

    public void setCreateTime(Date createTime) {
    
    
        this.createTime = createTime;
    }

    public ArticleMemento saveToMemento(){
    
    
        ArticleMemento articleMemento = new ArticleMemento(this.title,this.content,this.createTime);
        return articleMemento;
    }

    public void getArticleFromMemento(ArticleMemento articleMemento){
    
    
        this.title = articleMemento.getTitle();
        this.content = articleMemento.getContent();
        this.createTime = articleMemento.getCreateTime();
    }
}

Note: In addition to the getter/setter method, there is a method for backup and a method for restoration.

2. Create a class to store historical data. The information of this class must be the same as the original class, otherwise it cannot be fully backed up:

package com.zwx.design.pattern.memento;

import java.util.Date;

public class ArticleMemento {
    
    
    private String title;
    private String content;
    private Date createTime;

    public ArticleMemento(String title, String content, Date createTime) {
    
    
        this.title = title;
        this.content = content;
        this.createTime = createTime;
    }

    public String getTitle() {
    
    
        return title;
    }

    public void setTitle(String title) {
    
    
        this.title = title;
    }

    public String getContent() {
    
    
        return content;
    }

    public void setContent(String content) {
    
    
        this.content = content;
    }

    public Date getCreateTime() {
    
    
        return createTime;
    }

    public void setCreateTime(Date createTime) {
    
    
        this.createTime = createTime;
    }
}

3. Then a class is needed to manage historical snapshot information:

package com.zwx.design.pattern.memento;

import java.util.ArrayList;
import java.util.List;

public class ArticleCaretaker {
    
    
    private final List<ArticleMemento> list = new ArrayList<>();

    public ArticleMemento getArticle(int index){
    
    
        return list.get(index);
    }

    public void setArticle(ArticleMemento articleMemento){
    
    
        list.add(articleMemento);
    }
}

4. Finally, let's test it with a test class:

package com.zwx.design.pattern.memento;

import java.util.Date;

public class TestMemento {
    
    
    public static void main(String[] args) {
    
    
        ArticleCaretaker articleCaretaker = new ArticleCaretaker();

        ArticleText articleText = new ArticleText("标题1","内容1",new Date());
        ArticleMemento articleMemento = articleText.saveToMemento();
        articleCaretaker.setArticle(articleMemento);//备忘1次

        articleText = new ArticleText("标题2","内容2",new Date());
        System.out.println(String.format("修改后的标题为【%s】,内容为【%s】",articleText.getTitle(),articleText.getContent()));

        articleText.getArticleFromMemento(articleCaretaker.getArticle(0));
        System.out.println(String.format("还原后的标题为【%s】,内容为【%s】",articleText.getTitle(),articleText.getContent()));
    }
}

The output is as follows:

修改后的标题为【标题2】,内容为【内容2】
还原后的标题为【标题1】,内容为【内容1

This is an example of the memorandum pattern. This design pattern should be very simple.

Memo mode role

From the above example, we can conclude that the iterator mode has three main roles:

  • Originator role (Originator): Responsible for creating a memo to record the state that needs to be saved, and the rollback function of the state is required.
  • Memento role (Memento): used to store the internal state of the Originator role, and can prevent objects other than the Originator from accessing.
  • Caretaker: Responsible for storing and managing memo functions. And it should not be able to access the content of the memo itself.

Memo mode application scenarios

As a programmer, the memo mode should not have been used before. For example, svn, git, etc. provide historical version management functions. These are the manifestations of the memo mode. Memos can be used for rich text editors. Mode to achieve, as well as the server's snapshot function, we can also fall back to the historical state. Memo mode is generally used in the following scenarios:

  • 1. Scenes that need to save historical snapshots
  • 2. Want to save the state outside the object, and the specific saved content of the state cannot be accessed by other objects except yourself

Pros and Cons of Memo Mode

advantage:

  • 1. The responsibilities of the initiator are simplified, the storage and acquisition of the state are isolated, and the client does not need to care about the details of state preservation.
    Disadvantages:
  • 1. Consume resources. If the content of each snapshot is very large, it will consume a lot of memory.

to sum up

The memo pattern is a simple design pattern that is easy to be completely ignored. This article mainly introduces the basic use of the memo mode, and uses a simple example to help better understand the memo mode.

Please pay attention to me and learn and progress with the lone wolf .

Guess you like

Origin blog.csdn.net/zwx900102/article/details/109294498