一,C#设计模式:备忘录模式(Memento Pattern)

1、发起人角色(Originator):记录当前时刻的内部状态,负责创建和恢复备忘录数据。负责创建一个备忘录Memento,用以记录当前时刻自身的内部状态,并可使用备忘录恢复内部状态。Originator【发起人】可以根据需要决定Memento【备忘录】存储自己的哪些内部状态。
2、备忘录角色(Memento):负责存储发起人对象的内部状态,在进行恢复时提供给发起人需要的状态,并可以防止Originator以外的其他对象访问备忘录。备忘录有两个接口:Caretaker【管理角色】只能看到备忘录的窄接口,他只能将备忘录传递给其他对象。Originator【发起人】却可看到备忘录的宽接口,允许它访问返回到先前状态所需要的所有数据。
3、管理者角色(Caretaker):负责保存备忘录对象。负责备忘录Memento,不能对Memento的内容进行访问或者操作。

二,代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace _22.备忘录模式
{ /// <summary>
/// 部门--需要备份的数据,是状态数据,没有操作
/// </summary>
public sealed class Dept
{
/// <summary>
/// 部门
/// </summary>
public string DeptName { get; set; } /// <summary>
/// 人数
/// </summary>
public string Number { get; set; }
} /// <summary>
/// 发起人--相当于【发起人角色】Originator
/// </summary>
public sealed class DeptBackOriginator
{
// 发起人需要保存的内部状态
private List<Dept> _deptList; public List<Dept> DeptList
{
get
{
return this._deptList;
} set
{
this._deptList = value;
}
}
/// <summary>
/// 初始化需要备份的部门
/// </summary>
/// <param name="deptList"></param>
public DeptBackOriginator(List<Dept> deptList)
{
if (deptList != null)
{
this._deptList = deptList;
}
else
{
throw new ArgumentNullException("参数不能为空!");
}
} /// <summary>
/// 创建备忘录对象实例,将当期要保存的联系人列表保存到备忘录对象中
/// </summary>
/// <returns></returns>
public DeptMemento CreateMemento()
{
return new DeptMemento(new List<Dept>(this._deptList));
} /// <summary>
/// 将备忘录中的数据备份还原到部门列表中
/// </summary>
/// <param name="memento"></param>
public void RestoreMemento(DeptMemento memento)
{
this.DeptList = memento.DeptListBack;
}
/// <summary>
/// 显示备份包含的数据
/// </summary>
public void Show()
{
Console.WriteLine("部门列表中共有{0}个部门,他们是:", DeptList.Count);
foreach (Dept p in DeptList)
{
Console.WriteLine("部门: {0} 人数: {1}", p.DeptName, p.Number);
}
}
} /// <summary>
/// 备忘录对象,用于保存状态数据,保存的是当时对象具体状态数据--相当于【备忘录角色】Memeto
/// </summary>
public sealed class DeptMemento
{
/// <summary>
/// 保存发起人创建的部门数据
/// </summary>
public List<Dept> DeptListBack { get; private set; } public DeptMemento(List<Dept> deptList)
{
DeptListBack = deptList;
}
} /// <summary>
/// 管理角色,它可以管理【备忘录】对象,如果是保存多个【备忘录】对象,当然可以对保存的对象进行增、删等管理处理---相当于【管理者角色】Caretaker
/// </summary>
public sealed class MementoCaretaker
{
/// <summary>
/// 如果想保存多个【备忘录】对象,通过字典或者堆栈来保存
/// </summary>
public Dictionary<string, DeptMemento> DeptMementoDictionary { get; set; }
/// <summary>
/// 单个备份点
/// </summary>
public DeptMemento DeptMemento { get; set; }
public MementoCaretaker()
{
DeptMementoDictionary = new Dictionary<string, DeptMemento>();
} } class Program
{
static void Main(string[] args)
{
List<Dept> dept = new List<Dept>()
{
new Dept() { DeptName="技术部", Number = ""},
new Dept() { DeptName="人事部", Number = ""},
new Dept() { DeptName="产品部", Number = ""}
}; #region 单个备份点 //手机名单发起人
DeptBackOriginator mobileOriginator = new DeptBackOriginator(dept);
mobileOriginator.Show(); // 创建备忘录并保存备忘录对象
MementoCaretaker manager = new MementoCaretaker();
manager.DeptMemento = mobileOriginator.CreateMemento(); ///创建第一个备份点
manager.DeptMementoDictionary.Add("One", mobileOriginator.CreateMemento()); // 更改发起人联系人列表
Console.WriteLine("----移除最后一个联系人--------");
mobileOriginator.DeptList.RemoveAt();
mobileOriginator.Show(); ///创建第二个备份点
Thread.Sleep(); //等待10秒
manager.DeptMementoDictionary.Add("Two", mobileOriginator.CreateMemento()); // 恢复到原始状态
Console.WriteLine("-------恢复联系人列表------");
mobileOriginator.RestoreMemento(manager.DeptMemento);
mobileOriginator.Show();
#endregion #region 多个备份点
Console.WriteLine("-------输出两个备份点------");
////输出备份点
foreach (var item in manager.DeptMementoDictionary)
{
mobileOriginator.RestoreMemento(item.Value);
mobileOriginator.Show();
}
Console.WriteLine("-------恢复到第一个备份点------");
mobileOriginator.RestoreMemento(manager.DeptMementoDictionary["One"]);
mobileOriginator.Show();
Console.WriteLine("-------恢复到第二个备份点------");
mobileOriginator.RestoreMemento(manager.DeptMementoDictionary["Two"]);
mobileOriginator.Show();
#endregion Console.Read();
}
}
}

最新文章

  1. [crunch bang]在Crunch Bang安装和设置fcitx(小企鹅输入法)
  2. 转:TimeSpan的用法
  3. 深入浅出C++引用(Reference)类型
  4. java.lang.IllegalStateException
  5. DEPENDENT SUBQUERY” 和 “SUBQUERY”
  6. Injecting and Binding Objects to Spring MVC Controllers--转
  7. Qt String 与char* char int之间的转换
  8. Attempted to lock an already-locked dir异常解决方法
  9. 在java中使用MongoDB数据库
  10. OKMX6Q ffmpeg &amp; ffserver
  11. tcpdump完全指南
  12. 更改angular的默认端口
  13. git 提交规范
  14. 20145122《JAVA开发环境的熟悉》实验报告
  15. linux -- ubuntu14.10安装gnome桌面环境和dock工具
  16. 【目标检测大集合】R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记
  17. js惊奇效果分享,和排序算法
  18. popup menu案例,无说明只代码
  19. CodeForces - 348A Mafia (巧妙二分)
  20. Java中文乱码问题(转)

热门文章

  1. Python学习:Python设计模式-单例模式
  2. POJ 2155 Matrix (树状数组 &amp;&amp; 区间计数)
  3. Devexpress MVC Gridview
  4. POJ 1912 凸包
  5. 模板_SPLAY
  6. codevs 5971 打击犯罪 x
  7. SSM整合之---环境搭建
  8. pyinstaller打包的exe太大?你需要嵌入式python玄学
  9. Selenium 多窗口切换
  10. sklearn 的 PolynomialFeatures 的用法