设为首页 加入收藏

TOP

设计模式-行为型-备忘录模式(一)
2019-10-10 18:14:56 】 浏览:330
Tags:设计模式 行为 备忘录 模式

备忘录模式(Memento):

  后悔药来啦!!!备忘录模式在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便在需要时能将该对象恢复到原先保存的状态。

备忘录模式的角色:

  

  1)发起人(Originator):记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息。

  2)备忘录(Memento):负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。

  3)管理者(CareTaker):对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。

  根据上述UML图实现代码: 

 1 internal class Program
 2 {
 3     private static void Main(string[] args)
 4     {
 5         Originator o = new Originator();
 6         o.State = "On";
 7 
 8         Caretaker c = new Caretaker();
 9         c.Memento = o.CreateMemento();
10 
11         o.State = "Off";
12         o.SetMemento(c.Memento);
13     }
14 }
15 
16 /// <summary>
17 /// 备忘录
18 /// </summary>
19 public class Memento
20 {
21     private string _state;
22 
23     public Memento(string state)
24     {
25         this._state = state;
26     }
27 
28     public string State
29     {
30         get { return _state; }
31     }
32 }
33 
34 /// <summary>
35 /// 发起人
36 /// </summary>
37 public class Originator
38 {
39     private string _state;
40 
41     public string State
42     {
43         get
44         {
45             return _state;
46         }
47         set
48         {
49             _state = value;
50             Console.WriteLine("State = " + _state);
51         }
52     }
53 
54     public Memento CreateMemento()
55     {
56         return (new Memento(_state));
57     }
58 
59     public void SetMemento(Memento memento)
60     {
61         Console.WriteLine("Restoring state...");
62         State = memento.State;
63     }
64 }
65 
66 /// <summary>
67 /// 管理者
68 /// </summary>
69 public class Caretaker
70 {
71     private Memento _memento;
72 
73     public Memento Memento
74     {
75         get
76         {
77             return _memento;
78         }
79         set
80         {
81             _memento = value;
82         }
83     }
84 }

  现实生活中,我们往往会进行文档的备份或者SVN,git的快照。

  1 internal class Program
  2 {
  3     private static void Main(string[] args)
  4     {
  5         List<Document> docs = new List<Document>()
  6         {
  7             new Document{ Name="水浒传", Content="水浒传123131231"  },
  8             new Document{ Name="三国演义", Content="三国演义111111"  }
  9         };
 10 
 11         Originator originator = new Originator(docs);
 12         Caretaker caretaker = new Caretaker();
 13         DateTimeOffset time1 = new DateTimeOffset(DateTime.Now);
 14         caretaker.MementoDic.Add(time1, originator.CreateMemento());
 15         originator.GetShow();
 16         Console.WriteLine("====================================");
 17         Console.WriteLine("修改内容");
 18         docs[0].Content = "新水浒,搞笑ing";
 19         DateTimeOffset time2 = new DateTimeOffset(DateTime.Now);
 20         caretaker.MementoDic.Add(time2, originator.CreateMemento());
 21         originator.GetShow();
 22         Console.WriteLine("====================================");
 23         Console.WriteLine("回滚到第一阶段");
 24         originator.SetMemento(caretaker.MementoDic[time1]);
 25         originator.GetShow();
 26         Console.WriteLine("回滚到第二阶段");
 27         originator.SetMemento(caretaker.MementoDic[time2]);
 28         originator.GetShow();
 29     }
 30 }
 31 
 32 /// <summary>
 33 /// 文件
 34 /// </summary>
 35 public class Document
 36 {
 37     public string Name { get; set; }
 38     public string Content { get; set; }
 39 }
 40 
 41 /// <summary>
 42 /// 备忘录
 43 /// </summary>
 44 public class Memento
 45 {
 46     public List<Document> fileBack;
 47 
 48     public Memento(List<Document> docs)
 49     {
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ELK日志分析系统(1)-基本环境搭建 下一篇部分设计模式对比分析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目