美文网首页
设计模式(十九)-- 备忘录模式

设计模式(十九)-- 备忘录模式

作者: 信仰年輕 | 来源:发表于2018-12-05 16:22 被阅读0次

源代码
GitHub源代码

1.本文目标

本文目标是为了让大家认识并理解备忘录模式

2.基本套路

定义:保存某个对象的某个状态,以便于在适当的时候恢复对象
类型:行为型
选择关键点:是否可以在必要的时候捕捉到对象的内部状态
设计原则:遵循迪米特、开闭原则
使用概率:5%
难度系数:中

3.适用场景

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

4.使用步骤

用栗子能更好的说明问题,请继续往下看

5.举个栗子

我们用具体的代码去更好的理解这个设计模式

5.1栗子说明

  • 背景:学习的时候会记录自己的思路,然后会记下来,如果想修改,可以恢复到之前保存的状态
  • 目的:用备忘录模式模拟实现

5.2使用步骤

实现代码如下:

步骤1.创建手记

public class Article {

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

    public Article(String title, String content, String imgs) {
        this.title = title;
        this.content = content;
        this.imgs = 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 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 "Article{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", imgs='" + imgs + '\'' +
                '}';
    }
}

步骤2.创建手记备忘

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

步骤3.创建手记备忘管理者

public class ArticleMementoManager {

    private final Stack<ArticleMemento> ARTICLE_MEMENTO_STACK = new Stack<>();

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

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

步骤4. 测试

 public static void main(String[] args) {
        ArticleMementoManager articleMementoManager = new ArticleMementoManager();

        Article article = new Article("如影随行的设计模式A", "手记内容A", "手记图片A");
        ArticleMemento articleMemento = article.saveToMemento();
        articleMementoManager.addMemento(articleMemento);
        System.out.println("标题:" + article.getTitle() + " 内容:" + article.getContent() + " 图片:" + article.getImgs() + " 暂存成功");
        System.out.println("手记完整信息:" + article);

        System.out.println("修改手记start");

        article.setTitle("如影随行的设计模式B");
        article.setContent("手记内容B");
        article.setImgs("手记图片B");

        System.out.println("修改手记end");

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

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

        article.setTitle("如影随行的设计模式C");
        article.setContent("手记内容C");
        article.setImgs("手记图片C");

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

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

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


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

6.优点

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

7.缺点

  • 资源占用

8.总结

本文只是对备忘录模式进行一个分享,接下来会从创建型模式,结构型模式,行为型模式,这三大类展开一个系列分享,大家可以持续进行关注,信仰年輕的设计模式,蟹蟹啦。

相关文章

网友评论

      本文标题:设计模式(十九)-- 备忘录模式

      本文链接:https://www.haomeiwen.com/subject/oihwcqtx.html