B06 备忘录模式 示例

示例类图

备忘录模式.png

示例代码

状态需要被保存的类
  • 由此类创建备忘录;
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 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 + '\'' +
                '}';
    }

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

}
备忘录
  • 备忘录不允许 setter 方法,备忘录的状态只能通过构造器传入;
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 + '\'' +
                '}';
    }

}
备忘录管理者
  • 备忘录管理者和备忘录是聚合关系,两者生命周期并不一致;
import java.util.Stack;

public class ArticleMementoManager {

    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) {

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

输出:

标题:如影随行的设计模式A 内容:手记内容A 图片:手记图片A 暂存成功
手记完整信息:Article{title='如影随行的设计模式A', content='手记内容A', imgs='手记图片A'}
修改手记start
修改手记end
手记完整信息:Article{title='如影随行的设计模式B', content='手记内容B', imgs='手记图片B'}
暂存回退start
回退出栈1次
回退出栈2次
暂存回退end
手记完整信息:Article{title='如影随行的设计模式A', content='手记内容A', imgs='手记图片A'}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • “你敢面对自己内心真正的恐惧吗?”“你究竟是谁!”他看起来那么无助,双手捂着胸口,不停得喘着粗气。门外的几个人看着...
    安教主之韵阅读 404评论 0 1
  • 大家好,我是ZerK。 最近这几年明代哲学家王阳明的“阳明心学”得到了广泛的推广。 所以今天这篇文章带你走进王阳明...
    长安行动派阅读 2,012评论 0 0

友情链接更多精彩内容