[Design mode] 19, memo mode

(19) Memo Mode

Memento Pattern refers to capturing the internal state of an object and saving this state outside the object without breaking encapsulation. Similar to the snapshot mechanism.

1. Design principles of the memo pattern

1. Originator: Responsible for creating a memorandum to record the state that needs to be saved;

2. Memento: used to store the internal state of Originator, and can prevent objects other than Originator from accessing;

3. Caretaker: Responsible for the management and creation of memos.

2. Simple case

The implementation of the draft box is in the form of a memo. Through the draft box, you can restore the content you wrote before, and even restore it infinitely. In fact, in some scenarios, we can also consider using the form of an array instead of a stack, so that we can fall back to the specified version according to the index.

public class Editor {
    
    
    private String title;
    private String content;
    private String img;

	//get,set

    //    保存功能,实际是保存到备忘录
    public ArticleMemento saveToMemento() {
    
    
        ArticleMemento articleMemento = new ArticleMemento(this.title, this.content, this.img);
        return articleMemento;
    }

//    撤销功能就是从栈中取出上一个ArticleMemento对象
    public void undoFromMemento(ArticleMemento articleMemento) {
    
    
        this.title = articleMemento.getTitle();
        this.content = articleMemento.getContent();
        this.img = articleMemento.getImg();
    }

   //全参构造
   //重写tosting
}
public class ArticleMemento {
    
    
    private String title;
    private String content;
    private String img;

    public String getTitle() {
    
    
        return title;
    }

    public String getContent() {
    
    
        return content;
    }

    public String getImg() {
    
    
        return img;
    }
    
	//全参构造
   //重写tosting
    
}
public class DraftBox {
    
    
    private final Stack<ArticleMemento> STACK = new Stack<>();

    public ArticleMemento getMemento() {
    
    
        ArticleMemento articleMemento = null;
//        业务场景一:草稿实时保存,所以撤销指的是回滚到上一版本,并不是上一步。
//        第一次弹栈是当前对象,第二次弹栈是上一个对象。
//        articleMemento = STACK.pop();
//        articleMemento = STACK.pop();

//        业务场景二:及时撤销,用户在不保存的情况下撤销,即回退到上一步。
        articleMemento = STACK.pop();

        return articleMemento;
    }

    public void addMemento(ArticleMemento articleMemento) {
    
    
        STACK.push(articleMemento);
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        DraftBox draftBox = new DraftBox();
        Editor editor = new Editor("标题1", "备忘录模式", "abc.img");
        ArticleMemento articleMemento = editor.saveToMemento();
        draftBox.addMemento(articleMemento);
        System.out.println(editor.getTitle()+"\t"+editor.getContent()+"\t"+editor.getImg());

        System.out.println("修改标题");
        editor.setTitle("标题2");
        System.out.println("发生了修改后");
//        业务场景一:修改后并保存
       /* articleMemento = editor.saveToMemento();
        draftBox.addMemento(articleMemento);*/
        System.out.println(editor.getTitle()+"\t"+editor.getContent()+"\t"+editor.getImg());

//        测试业务场景一
        articleMemento = draftBox.getMemento();
        editor.undoFromMemento(articleMemento);
        System.out.println("执行撤销指令后");
        System.out.println(editor.getTitle()+"\t"+editor.getContent()+"\t"+editor.getImg());
    }
}
image-20210319090547678

3. Comments on the memo mode

The memo mode is very suitable for business scenarios that need to be rolled back, but it can be seen that a member variable of an operation object needs to be maintained in Memento, which can easily make the system complicated.

Guess you like

Origin blog.csdn.net/qq_40589204/article/details/118511820