設計模式備忘錄模式 ( 簡介 | 適用場景 | 優缺點 | 代碼示例 )

      網友投稿 1072 2022-05-29

      文章目錄

      一、備忘錄模式簡介

      二、備忘錄模式 適用場景

      三、備忘錄模式 優缺點

      四、備忘錄模式 與 狀態模式

      五、備忘錄模式 代碼示例

      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 mArticleMementoStack = new Stack<>(); /** * 獲取棧頂的備忘錄信息 * @return */ public ArticleMemento getArticleMemento() { return mArticleMementoStack.pop(); } /** * 備忘錄信息入棧 * 放在棧頂 * @param articleMemento */ public void setArticleMemento(ArticleMemento articleMemento) { mArticleMementoStack.push(articleMemento); } }

      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小時內刪除侵權內容。

      上一篇:AJAX學習筆記05
      下一篇:linux下的find文件查找命令與grep文件內容查找命令
      相關文章
      亚洲视频精品在线| 亚洲91av视频| 亚洲国产精品午夜电影| 亚洲精品无码乱码成人| 亚洲国产精品毛片av不卡在线| 亚洲av日韩av永久在线观看| 久久久久久亚洲精品影院| 亚洲乱码在线观看| 亚洲影视自拍揄拍愉拍| 亚洲乱码一二三四区麻豆| 亚洲人成人77777网站不卡| 亚洲国产综合在线| 亚洲a级成人片在线观看| 亚洲H在线播放在线观看H| 亚洲精品伊人久久久久| 亚洲av无码电影网| 在线综合亚洲中文精品| 亚洲精品无码一区二区| 亚洲AV女人18毛片水真多| 亚洲 另类 无码 在线| 亚洲国产一区二区三区| 久久精品国产精品亚洲| 久久久精品国产亚洲成人满18免费网站 | 91亚洲精品第一综合不卡播放| 亚洲欧洲国产日韩精品| 亚洲欧洲日产韩国在线| 2020久久精品亚洲热综合一本 | 亚洲AV午夜成人片| 亚洲伊人tv综合网色| 亚洲视频一区在线播放| 亚洲不卡在线观看| 亚洲成熟丰满熟妇高潮XXXXX| 国产成人亚洲精品蜜芽影院| 久久精品国产亚洲精品| 亚洲AV日韩AV天堂久久 | 综合亚洲伊人午夜网 | 亚洲福利在线视频| 亚洲ts人妖网站| 亚洲av午夜电影在线观看| 国产成人精品日本亚洲专区 | 亚洲AV无码一区二区三区电影|