备忘录模式

一、备忘录模式介绍

1、定义与类型

定义:保存一个对象的某个状态,以便在适当的时候恢复对象。
“后悔药"
类型:行为型

2、适用场景

保存及恢复数据相关业务场景
后悔的时候,即想恢复到之前的状态

3、优点

为用户提供一种可恢复机制
存档信息的封装

4、缺点

资源占用

5、相关设计模式

备忘录模式和状态模式

二、代码示例

模拟场景:在网站上发布手记,暂存手记的不同版本

手记类:

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

    public String getTitle() {
        return title;
    }

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

    public String getContent() {
        return content;
    }

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

    public String getImgs() {
        return imgs;
    }

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

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

    public ArticleMemento saveToMemento(){
        ArticleMemento articleMemento = new ArticleMemento(this);
        return articleMemento;
    }

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

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

存档类:

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

    public ArticleMemento(Article article) {
        this.title = article.getTitle();
        this.content = article.getContent();
        this.imgs = article.getImgs();
    }

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

存档管理类:

public class ArticleMementorManager {
    private final Stack<ArticleMemento> ARTICLE_MEMENTO_STACK = new Stack<ArticleMemento>();

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

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

测试类:

public class Test {

    public static void main(String[] args) {
        ArticleMementorManager articleMementorManager = new ArticleMementorManager();

        Article article = new Article("如影随形的手记A", "内容A", "图片A");

        ArticleMemento articleMemento = article.saveToMemento();
        articleMementorManager.addMemento(articleMemento);

        System.out.println("标题:" + article.getTitle() + ",内容:" + article.getContent() + ",图片:" + article.getImgs());
        System.out.println("完整笔记信息:" + article);

        System.out.println("-分割线------------------------------------");

        System.out.println("修改手记start");
        article.setTitle("如影随形的手记B");
        article.setContent("内容B");
        article.setImgs("图片B");
        System.out.println("修改手记end");

        articleMemento = article.saveToMemento();
        articleMementorManager.addMemento(articleMemento);

        System.out.println("标题:" + article.getTitle() + ",内容:" + article.getContent() + ",图片:" + article.getImgs());
        System.out.println("完整笔记信息:" + article);

        System.out.println("-分割线------------------------------------");

        article.setTitle("如影随形的手记C");
        article.setContent("内容C");
        article.setImgs("图片C");

        System.out.println("暂存回退start");

        System.out.println("回退出栈1次");
        articleMemento = articleMementorManager.getMemento();
        article.undoFromMemento(articleMemento);

        System.out.println("完整笔记信息:" + article);

        System.out.println("回退出栈2次");
        articleMemento = articleMementorManager.getMemento();
        article.undoFromMemento(articleMemento);

        System.out.println("暂存回退end");
        System.out.println("完整笔记信息:" + article);
    }

}

输出:
标题:如影随形的手记A,内容:内容A,图片:图片A
完整笔记信息:Article{title='如影随形的手记A', content='内容A', imgs='图片A'}
-分割线------------------------------------
修改手记start
修改手记end
标题:如影随形的手记B,内容:内容B,图片:图片B
完整笔记信息:Article{title='如影随形的手记B', content='内容B', imgs='图片B'}
-分割线------------------------------------
暂存回退start
回退出栈1次
完整笔记信息:Article{title='如影随形的手记B', content='内容B', imgs='图片B'}
回退出栈2次
暂存回退end
完整笔记信息:Article{title='如影随形的手记A', content='内容A', imgs='图片A'}

三、源码示例

1、spring中的webflow

posted @ 2020-07-01 09:09  weixiaokun  阅读(329)  评论(0编辑  收藏  举报