【設計模式】備忘錄模式 ( 簡介 | 適用場景 | 優缺點 | 代碼示例 )
文章目錄
一、備忘錄模式簡介
二、備忘錄模式 適用場景
三、備忘錄模式 優缺點
四、備忘錄模式 與 狀態模式
五、備忘錄模式 代碼示例
1、文檔類
2、文檔備忘錄類
3、文檔備忘錄管理類
4、測試類
一、備忘錄模式簡介
備忘錄模式 : 保存
對象
的
某個狀態
, 以便在 適當的時候
恢復對象
;
( 形象的比喻 : " 后悔藥 " )
如 : 游戲存檔 , 一些編輯工具中的 "
撤銷
" 操作 , 瀏覽器中的
后退 ;
備忘錄模式 類型 : 行為型 ;
二、備忘錄模式 適用場景
備忘錄模式 適用場景 :
撤銷操作 :
保存 / 恢復 數據
的相關業務場景 ;
如 : 在 Word 中編寫文檔 , 如果想要撤銷之前的 輸入 / 刪除操作 , 使用 Ctrl + Z 執行 " 撤銷 " 操作 ;
狀態恢復 : 在 " 后悔 " 的時候 ,
將對象恢復到之前的狀態 ;
如 : 游戲中的存檔使用 ;
三、備忘錄模式 優缺點
備忘錄模式 優點 :
存檔信息 : 封裝
存檔信息 ;
恢復機制 : 為 用戶 提供一種
可恢復 機制 ;
先封裝 存檔信息
,
然后才可以提供 可恢復機制
;
封裝的 對象的狀態 , 就是
對象中 各個屬性的屬性值 , 快照 ;
備忘錄模式 缺點 :
資源占用
, 會額外 占用 磁盤 / 內存 等資源 ;
四、備忘錄模式 與 狀態模式
備忘錄模式 與 狀態模式 :
備忘錄模式狀態表示 : 備忘錄模式 中 , 使用
對象實例
表示狀態 , 當前對象的 存檔 是該對象的實例 ;
狀態模式狀態表示 : 狀態模式 中 , 使用
類
表示狀態 ;
五、備忘錄模式 代碼示例
業務場景 : 編輯文檔 , 有暫存功能 , 暫時先保存到內存中 ;
1、文檔類
package memento; /** * 文檔 * 需要保存的對象 */ public class Article { private String title; private String content; private String image; public Article(String tittle, String content, String image) { this.title = tittle; this.content = content; this.image = image; } 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 getImage() { return image; } public void setImage(String image) { this.image = image; } /** * 保存信息到備忘錄 * @return */ public ArticleMemento saveToMemento() { ArticleMemento articleMemento = new ArticleMemento(title, content, image); return articleMemento; } /** * 從備忘錄恢復 * @param articleMemento */ public void undoFromMemento(ArticleMemento articleMemento) { this.title = articleMemento.getTitle(); this.content = articleMemento.getContent(); this.image = articleMemento.getImage(); } @Override public String toString() { return "Article{" + "title='" + title + '\'' + ", content='" + content + '\'' + ", image='" + image + '\'' + '}'; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2、文檔備忘錄類
package memento; /** * 文檔的備忘錄類 * 主要用于存儲文檔的各種屬性狀態信息 * 備忘錄 快照 沒有 set 方法 * 只能通過構造函數設置備忘錄數據 */ public class ArticleMemento { private String title; private String content; private String image; public ArticleMemento(String title, String content, String image) { this.title = title; this.content = content; this.image = image; } public String getTitle() { return title; } public String getContent() { return content; } public String getImage() { return image; } @Override public String toString() { return "ArticleMemento{" + "title='" + title + '\'' + ", content='" + content + '\'' + ", image='" + image + '\'' + '}'; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
3、文檔備忘錄管理類
package memento; import java.util.Stack; /** * 備忘錄管理類 */ public class ArticleMementoManager { /** * 存儲所有的備忘錄信息 * 在 棧 數據結構中存儲 , 特點后進先出 */ private final Stack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
4、測試類
package memento; public class Main { public static void main(String[] args) { ArticleMementoManager articleMementoManager = new ArticleMementoManager(); // 創建并輸入文檔內容 Article article = new Article("標題", "內容", "圖片鏈接"); // 保存備忘錄信息 ArticleMemento articleMemento = article.saveToMemento(); // 將備忘錄信息設置到 備忘錄管理者 articleMementoManager.setArticleMemento(articleMemento); // 打印備忘錄內容 System.out.println("文檔信息 : " + article.toString()); // 修改文檔內容 article.setTitle("標題 2"); article.setContent("內容 2"); article.setImage("圖片鏈接 2"); // 保存新的備忘錄信息 articleMemento = article.saveToMemento(); // 將備忘錄信息設置到 備忘錄管理者 articleMementoManager.setArticleMemento(articleMemento); // 打印備忘錄內容 System.out.println("文檔信息 : " + article.toString()); // 此時 ArticleMementoManager 中存儲了 2 個存檔 // 存檔 1 : Article{title='標題', content='內容', image='圖片鏈接'} // 存檔 2 : Article{title='標題 2', content='內容 2', image='圖片鏈接 2'} // 使用備忘錄回退 // 先將棧頂的當前備忘錄出棧 , 移除 articleMementoManager.getArticleMemento(); // 然后獲取上一個備忘錄 , 并設置到 Article 中 article.undoFromMemento(articleMementoManager.getArticleMemento()); // 打印備忘錄內容 System.out.println("文檔信息 : " + article.toString()); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
5G游戲
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。