设计模式课程 设计模式精讲 22-2 备忘录模式coding

我只是一个虾纸丫 提交于 2020-02-24 06:39:52

1    代码演练

1.1  代码演练1

 

 

 

 

1    代码演练

1.1  代码演练1

需求:

网站笔记需要存储快照,能实现回退的功能。

 

注意:

a  设计的时候,可以分为笔记类,笔记快照类和 笔记快照管理类  三个类。

b  笔记快照管理 和 笔记快照是聚合关系,快照管理可以有0到多个笔记快照。

c  快照管理类用栈的方式存储快照,先进后出,后进先出。

 

uml类图:(右边是测试类)

 

 

 

 

测试类:

 

package com.geely.design.pattern.behavioral.memento;

/**
 * Created by geely
 */
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);

    }
}

 

 

 

 

笔记类:

package com.geely.design.pattern.behavioral.memento;

/**
 * Created by geely
 */
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 + '\'' +
                '}';
    }
}

 

 

笔记快照类:

 

package com.geely.design.pattern.behavioral.memento;

/**
 * Created by geely
 */
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.geely.design.pattern.behavioral.memento;

import java.util.Stack;

/**
 * Created by geely
 */
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);
    }

}

 

 

 

 

打印日志:

 

标题:如影随行的设计模式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'}

Process finished with exit code 0

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!