Design Patterns - memo mode

First, the basic introduction

Memo mode (behavioral): without violating encapsulation, capture and the internal state of an object, and save the state of the object outside, so that later when an object needs to be able to restore the previously saved state.

Second, a role that includes

1. The sponsor role: record the internal state information of current time, offer to create memos and memo data recovery function, the realization of other business functions, it can access all the information in the memo.

2. Memorandum Role: responsible for storing the internal state sponsors, these internal states when needed to sponsor.

3. Memorandum of management, to provide preservation and access the memo function, but it can not be accessed with a modification of the contents of the memo.

 

Third, cases and UML class diagrams

Case Description:

           When we operate a word document, total habitual save operation press ctrl + s, when a mistake to roll back to the previous step or more on one step.

 

UML 类图:

Class Word:

public class Word {

    //word内容
    private StringBuilder content = new StringBuilder();

    /**
     * 保存当前步骤
     * @return 备忘录
     */
    public Memento saveContent() {
        return new Memento(content.toString());
    }

    /**
     * 回滚
     * @param memento 回滚的内容
     */
    public void rollBack(Memento memento) {
        this.content = new StringBuilder();
        this.content.append(memento.getContent()).append("\n");
    }

    public void write(String content) {
        this.content.append(content);
    }

    public String show() {
        return content.toString();
    }
}

Description: The document class, sponsor role, providing saving and rollback functions and write functions on the current document.

Class Memento:

public class Memento {

    //文档内容
    private String content;

    public Memento(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

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

Description: Class memo, memo role, save the contents of a word document a moment in time.

Deliverable MementoManage:

public class MementoManage {

    private LinkedList<Memento> mementoList = new LinkedList<>();

    /**
     * 添加一个备忘录即保存内容
     * @param memento 备忘录
     */
    public void addMemento(Memento memento) {
        mementoList.add(memento);
    }

    /**
     * 获取上一步保存的内容
     * @return 备忘录
     */
    public Memento previousStep() {
        return mementoList.removeLast();
    }
}

Description: memo management class, role manager, to save and retrieve the memo, the memo can not be modified.

Class MementoTest:

public class MementoTest {

    public static void main(String[] args) {
        //备忘录管理
        MementoManage mementoManage = new MementoManage();
        //创建一个文档准备写内容
        Word word = new Word();
        word.write("hello word!");
        //保存内容
        Memento memento = word.saveContent();
        mementoManage.addMemento(memento);
        word.write("jaav");
        //继续写内容
        word.write("我要学java");
        //发现之前的java打错了,返回上一步
        //获取上一步保存的内容
        Memento previousStep = mementoManage.previousStep();
        //word进行回滚
        word.rollBack(previousStep);
        //输出回滚后的内容
        System.out.println(word.show());
    }
}

Description: Test and clients.

 

Fourth, application scenarios

1. The need to save / restore data related to the state scene

2. Provide a rollback operation.

Published 39 original articles · won praise 61 · views 60000 +

Guess you like

Origin blog.csdn.net/m0_37914588/article/details/103947780