java设计模式之备忘录模式

备忘录模式的定义:

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

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

备忘录模式的应用场景:

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

备忘录模式的UML类图:

 

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

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

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

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

public class Editor {

    private String title;
     String content;
     String imgs;

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

     String getTitle() {
        return title;
    }

     String getContent() {
         content;
    }

     String getImgs() {
        void setTitle(String title) {
         setContent(String content) {
         setImgs(String imgs) {
         ArticleMemento saveToMemento(){
        ArticleMemento articleMemento = new ArticleMemento(this.title,this.content,1)">this.imgs);
         articleMemento;
    }

     undoFromMemento(ArticleMemento articleMemento){
         articleMemento.getTitle();
         articleMemento.getContent();
         articleMemento.getImgs();
    }

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

然后创建备忘录角色:

 ArticleMemento {
     ArticleMemento(String title,1)"> imgs;
    }

    @Override
    return "ArticleMemento{" +
                "title='" + title + '\'' +
                ",1)">;
    }
}

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

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

     ArticleMemento getMemento(){
        ArticleMemento articleMemento = STACK.pop();
         addMemento(ArticleMemento articleMemento){
        STACK.push(articleMemento);
    }

}

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

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

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

        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("==========首次修改文章完成===========" editor);

        articleMemento = editor.saveToMemento();

        draftsBox.addMemento(articleMemento);

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


        System.out.println("==========第2次修改文章===========");
        editor.setTitle("手写Spring");
        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次撤销===========" editor);
        System.out.println("==========第2次撤销完成===========");

    }
}

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

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

备忘录模式的优点:

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

备忘录模式的缺点:

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

相关文章

什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
单一职责原则定义(Single Responsibility Principle,SRP)...
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...