備忘錄模式

1、備忘錄模式介紹

一、定義與類型

定義:保存一個對象的某個狀態,以便在適當的時候恢復對象。
「後悔藥"
類型:行爲型web

二、適用場景

保存及恢復數據相關業務場景
後悔的時候,即想恢復到以前的狀態spring

三、優勢

爲用戶提供一種可恢復機制
存檔信息的封裝設計模式

四、缺點

資源佔用ide

五、相關設計模式

備忘錄模式和狀態模式測試

2、代碼示例

模擬場景:在網站上發佈手記,暫存手記的不一樣版本網站

手記類:this

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

存檔管理類:code

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

3、源碼示例

一、spring中的webflow

相關文章
相關標籤/搜索