设计模式-备忘录模式

2024-06-13  本文已影响0人  灵台悠步
备忘录模式

备忘录模式(Memento Pattern),又成为快照模式(Snapshot Pattern),或者令牌模式(Token Pattern),是指在不破坏封装得前提下,捕获一个对象得内部状态,并在对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存得状态。属于行为型模式。
特征:后悔药

适用场景
优点
缺点
角色:
示例代码:对一篇文章得修改与撤销
发起人角色:
package com.caozz.demo2.memento.demo;

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 + '\'' +
                '}';
    }
}

备忘录角色
package com.caozz.demo2.memento.demo;

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 + '\'' +
                '}';
    }
}

备忘录管理者角色
package com.caozz.demo2.memento.demo;

import java.util.Stack;

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);
    }

}

测试
package com.caozz.demo2.memento.demo;

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

        Editor editor = new Editor("初始title",
                "初始文本",
                "111111.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("首次修改title");
        editor.setContent("首次修改文本");

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

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

        articleMemento = editor.saveToMemento();

        draftsBox.addMemento(articleMemento);

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


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

    }
}

测试结果
标题:初始title
内容:初始文本
插图:111111.png
暂存成功
完整的信息Editor{title='初始title', content='初始文本', imgs='111111.png'}
==========首次修改文章===========
==========首次修改文章完成===========
完整的信息Editor{title='首次修改title', content='首次修改文本', imgs='111111.png'}
==========保存到草稿箱===========
==========第2次修改文章===========
完整的信息Editor{title='二次修改title', content='二次修改文本', imgs='111111.png'}
==========第2次修改文章完成===========
==========第1次撤销===========
完整的信息Editor{title='首次修改title', content='首次修改文本', imgs='111111.png'}
==========第1次撤销完成===========
==========第2次撤销===========
完整的信息Editor{title='初始title', content='初始文本', imgs='111111.png'}
==========第2次撤销完成===========
上一篇 下一篇

猜你喜欢

热点阅读