一、備忘錄模式介紹
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

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/4536.html
標籤:設計模式
上一篇:單例模式
下一篇:命令模式
