java设计模式之备忘录模式

备忘录模式的定义:

  备忘录模式又叫作快照模式或者令牌模式,指在不破坏封装的前提下,捕获一个状态的内部状态,并在对象之前保存这个状态。这样以后就可

将该对象恢复到原先保存的状态,属于行为型设计模式。

备忘录模式的应用场景:

  • 需要保存历史快照的场景。
  • 希望在对象之外保存状态,且除了自己,其他类对象无法访问状态保存的具体内容。

备忘录模式的UML类图:

 

 由上图可以看到,备忘录模式主要包含3个角色。

  1. 发起人角色(Originator):负责创建一个备忘录,记录自身需要保存的状态,具备状态回滚功能。
  2. 备忘录角色(Memento):用于存储Originator的内部状态,且可以防止Originator以外的对象进行访问。
  3. 备忘录管理员(Caretaker):负责存储、提供管理Memento,无法对Memento的内容进行操作和访问。

使用备忘录模式实现草稿箱功能:

  首先创建发起人角色编辑器类:

public class Editor {

    private String title;
    private String content;
    private String imgs;

    public Editor(String title, String content, String imgs) {
        this.title = title;
        this.content = content;
        this.imgs = imgs;
    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

    public String getImgs() {
        return imgs;
    }

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

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

    public void setImgs(String imgs) {
        this.imgs = imgs;
    }

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

    public void undoFromMemento(ArticleMemento articleMemento){
        this.title = articleMemento.getTitle();
        this.content = articleMemento.getContent();
        this.imgs = articleMemento.getImgs();
    }

    @Override
    public String toString() {
        return "Editor{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", imgs='" + imgs + '\'' +
                '}';
    }
}

然后创建备忘录角色:

public class ArticleMemento {
    private String title;
    private String content;
    private String imgs;

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

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

    public String getImgs() {
        return imgs;
    }

    @Override
    public String toString() {
        return "ArticleMemento{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", imgs='" + imgs + '\'' +
                '}';
    }
}

接着创建备忘录管理角色草稿箱类,该类中的Stack类是Vector的一个子类,它实现了一个标准的后进先出的栈。

public class DraftsBox {
    private final Stack<ArticleMemento> STACK = new Stack<ArticleMemento>();

    public ArticleMemento getMemento(){
        ArticleMemento articleMemento = STACK.pop();
        return articleMemento;
    }

    public void addMemento(ArticleMemento articleMemento){
        STACK.push(articleMemento);
    }

}

最后编写客户端测试代码:

public class Test {
    public static void main(String[] args) {
        DraftsBox draftsBox = new DraftsBox();

        Editor editor = new Editor("我是这样手写Spring的,麻雀虽小五脏俱全",
                "本文节选自《Spring5核心原理与30个类手写实战》一书,Tom著,电子工业出版社出版。",
                "35576a9ef6fc407aa088eb8280fb1d9d.png");

        ArticleMemento articleMemento = editor.saveToMemento();
        draftsBox.addMemento(articleMemento);

        System.out.println("标题:" + editor.getTitle() + "\n" +
                            "内容:" + editor.getContent() + "\n" +
                            "插图:" + editor.getImgs() + "\n暂存成功");

        System.out.println("完整的信息" + editor);


        System.out.println("==========首次修改文章===========");
        editor.setTitle("【Tom原创】我是这样手写Spring的,麻雀虽小五脏俱全");
        editor.setContent("本文节选自《Spring5核心原理与30个类手写实战》一书,Tom著");

        System.out.println("==========首次修改文章完成===========");

        System.out.println("完整的信息" + editor);

        articleMemento = editor.saveToMemento();

        draftsBox.addMemento(articleMemento);

        System.out.println("==========保存到草稿箱===========");


        System.out.println("==========第2次修改文章===========");
        editor.setTitle("手写Spring");
        editor.setContent("本文节选自《Spring5核心原理与30个类手写实战》一书,Tom著");
        System.out.println("完整的信息" + editor);
        System.out.println("==========第2次修改文章完成===========");

        System.out.println("==========第1次撤销===========");
        articleMemento = draftsBox.getMemento();
        editor.undoFromMemento(articleMemento);
        System.out.println("完整的信息" + editor);
        System.out.println("==========第1次撤销完成===========");


        System.out.println("==========第2次撤销===========");
        articleMemento = draftsBox.getMemento();
        editor.undoFromMemento(articleMemento);
        System.out.println("完整的信息" + editor);
        System.out.println("==========第2次撤销完成===========");

    }
}

  在测试类中,我们首先创建一个管理角色对象,然后创建一个创建人角色编辑器,并赋值,相当于编辑一部分内容的状态,然后将当前的内容

保存到备忘录角色对象,然后通过管理角色压入到栈中。然后继续编辑,继续保存到备忘录。当撤销时,又以此还原到原来的状态。

备忘录模式的优点:

  • 简化发起人实体类的职责,隔离状态存储于获取,实现了信息的封装,客户端无须关心状态的保存细节。
  • 提供状态回滚功能。

备忘录模式的缺点:

  备忘录模式的缺点主要是消耗资源。如果需要保存的状态过多,则每一次保存都会消耗很多内存。

posted @ 2020-11-19 22:27  负重前行的小牛  阅读(205)  评论(0编辑  收藏  举报