设计模式备忘录模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

网友投稿 857 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小时内删除侵权内容。

上一篇:Linux Stat 命令:如何识别文件属性
下一篇:张小白教你在WSL的Ubuntu 20.04上一键安装MindSpore GPU版本
相关文章