备忘录模式

1.定义:保存一个对象的某个状态,以便在适当的时候恢复对象,即“后悔药”。

2.类型:行为型

3.适用场景:保存及恢复数据相关业务场景;后悔的时候,即想恢复到之前的状态。

4.优点:为用户提供一种可恢复机制;存档信息的封装。

5.缺点:资源占用

6.相关设计模式:状态模式

7.实例目录package

8.实例UML类图

9.代码

 1 package com.geely.design.pattern.behavioral.memento;
 2 
 3 public class Article {
 4     private String title;
 5     private String content;
 6     private String imgs;
 7 
 8     public Article(String title, String content, String imgs) {
 9         this.title = title;
10         this.content = content;
11         this.imgs = imgs;
12     }
13 
14     public String getTitle() {
15         return title;
16     }
17 
18     public void setTitle(String title) {
19         this.title = title;
20     }
21 
22     public String getContent() {
23         return content;
24     }
25 
26     public void setContent(String content) {
27         this.content = content;
28     }
29 
30     public String getImgs() {
31         return imgs;
32     }
33 
34     public void setImgs(String imgs) {
35         this.imgs = imgs;
36     }
37 
38     public ArticleMemento saveToMemento(){
39         ArticleMemento articleMemento = new ArticleMemento(this.title,this.content,this.imgs);
40         return articleMemento;
41     }
42 
43     public void undoFromMemento(ArticleMemento articleMemento){
44         this.title = articleMemento.getTitle();
45         this.content = articleMemento.getContent();
46         this.imgs = articleMemento.getImgs();
47     }
48 
49     @Override
50     public String toString() {
51         return "Article{" +
52                 "title='" + title + "'" +
53                 ", content='" + content +"'" +
54                 ", imgs='" + imgs + "'" +
55                 "}";
56     }
57 }
 1 package com.geely.design.pattern.behavioral.memento;
 2 
 3 public class ArticleMemento {
 4     private String title;
 5     private String content;
 6     private String imgs;
 7 
 8     public ArticleMemento(String title, String content, String imgs) {
 9         this.title = title;
10         this.content = content;
11         this.imgs = imgs;
12     }
13 
14     public String getTitle() {
15         return title;
16     }
17 
18     public String getContent() {
19         return content;
20     }
21 
22     public String getImgs() {
23         return imgs;
24     }
25     @Override
26     public String toString() {
27         return "ArticleMemento{" +
28                 "title='" + title + "'" +
29                 ", content='" + content +"'" +
30                 ", imgs='" + imgs + "'" +
31                 "}";
32     }
33 }
 1 package com.geely.design.pattern.behavioral.memento;
 2 
 3 import java.util.Stack;
 4 
 5 public class ArticleMementoManager {
 6 
 7     private final Stack<ArticleMemento> ARTICLE_MEMENTO = new Stack<>();
 8     public ArticleMemento getMemento(){
 9         ArticleMemento articleMemento = ARTICLE_MEMENTO.pop();
10         return articleMemento;
11     }
12 
13     public void addMemento(ArticleMemento articleMemento){
14         ARTICLE_MEMENTO.push(articleMemento);
15     }
16 }
 1 package com.geely.design.pattern.behavioral.memento;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         ArticleMementoManager articleMementoManager = new ArticleMementoManager();
 6         Article article = new Article("如影随行的设计模式A","手记内容A","手记图片A");
 7         ArticleMemento articleMemento = article.saveToMemento();
 8         articleMementoManager.addMemento(articleMemento);
 9         System.out.println("标题:"+article.getTitle()+"内容:"+article.getContent()+"图片:" + article.getImgs());
10         System.out.println("手记完整信息:" + article);
11 
12         System.out.println("修改手记start");
13         article.setTitle("如影随行的设计模式B");
14         article.setContent("手记内容B");
15         article.setImgs("手记图片B");
16         System.out.println("修改手记end");
17         System.out.println("手记完整信息:" + article);
18 
19         articleMemento = article.saveToMemento();
20         articleMementoManager.addMemento(articleMemento);
21 
22         System.out.println("暂存回退start");
23         System.out.println("回退出栈1次");
24         articleMemento = articleMementoManager.getMemento();
25         article.undoFromMemento(articleMemento);
26         System.out.println("手记完整信息:" + article);
27 
28         System.out.println("回退出栈2次");
29         articleMemento = articleMementoManager.getMemento();
30         article.undoFromMemento(articleMemento);
31 
32         System.out.println("暂存回退end");
33         System.out.println("手记完整信息:" + article);
34     }
35 }

 

posted @ 2019-01-06 17:38  逢春  阅读(161)  评论(0编辑  收藏  举报