设计模式-备忘录模式
一、概念
①什么是备忘录模式
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。在需要的时候可以将对象恢复到原来保存的状态。
②备忘录模式主要解决了什么问题
备忘录模式起到了一个恢复数据的作用,如果涉及到撤销,恢复数据等操作可以考虑使用备忘录模式。
③备忘录如何解决这个问题的
通过创建一个备忘录来存储对象的状态
④主要符合哪种设计原则
符合Law of Demeter(迪米特原则),通过增加一个管理备忘录的类(备忘录管理类),客户端类不与备忘录类耦合,主要与备忘录管理类耦合
二、备忘录模式类图
三、实战演练,管理员修改基础数据实现撤销功能。
①类图
②代码实现
PriceOriginator类
public class PriceOriginator { //临时用户单价 private decimal tempMember; public decimal TempMember { get { return tempMember; } set { tempMember = value; } } //VIP用户单价 private decimal vipMember; public decimal VipMember { get { return vipMember; } set { vipMember = value; } } //最少上机金额 private decimal minMoney; public decimal MinMoney { get { return minMoney; } set { minMoney = value; } } /// <summary> /// 创建备忘录,将当前需要保存的数据导入PriceMemeto对象 /// </summary> /// <returns></returns> public PriceMemeto SaveState() { return (new PriceMemeto(tempMember, vipMember, minMoney)); } /// <summary> /// 恢复数据 /// </summary> /// <param name="memeto">状态类对象</param> public void RecoveryState(PriceMemeto memeto) { this.minMoney = memeto.MinMoney; this.tempMember = memeto.TempMember; this.vipMember = memeto.VipMember; } /// <summary> /// 返回备忘录的数据 /// </summary> /// <returns></returns> public PriceModel Result() { PriceModel price = new PriceModel(); price.minMondey = minMoney; price.vipMember = vipMember; price.tempMember = tempMember; return price; } }
PriceMemento类
public class PriceMemeto { private decimal tempMember; private decimal vipMember; private decimal minMoney; /// <summary> /// 构造方法将定价信息传入 /// </summary> /// <param name="tempMember">临时用户单价</param> /// <param name="vipMember">VIP用户单价</param> /// <param name="minMoney">最少上机金额</param> public PriceMemeto (decimal tempMember,decimal vipMember,decimal minMoney) { this.minMoney = minMoney; this.tempMember = tempMember; this.vipMember = vipMember; } //临时用户单价 public decimal TempMember { get { return tempMember; } set { tempMember = value; } } //VIP用户单价 public decimal VipMember { get { return vipMember; } set { vipMember = value; } } //最少上机金额 public decimal MinMoney { get { return minMoney; } set { minMoney = value; } } }
PriceCaretaker类
public class PriceCaretaker { private PriceMemeto memeto; /// <summary> /// 得到备忘录的设置 /// </summary> public PriceMemeto Memeto { get { return memeto ; } set { memeto = value; } } }
业务逻辑层应用
保存对象状态
//实例化定价信息管理者类 static Memeto.PriceCaretaker priceCaretaker = new Memeto.PriceCaretaker(); //实例化定价信息发起人类 Memeto.PriceOriginator priceOriginator = new Memeto.PriceOriginator(); /// <summary> /// 查询定价信息 /// </summary> /// <returns></returns> public PriceModel SelectPrice() { //声明一个PriceModel对象来接收查询定价信息的结果 PriceModel price= client.SelectPrice(); priceOriginator.MinMoney = price.minMondey; priceOriginator.TempMember = price.tempMember; priceOriginator.VipMember = price.vipMember; //保存当前定价信息 priceCaretaker.Memeto = priceOriginator.SaveState(); return price; }
实现撤销
/// <summary> /// 撤销 /// </summary> /// <returns>定价信息</returns> public PriceModel RecoveryState() { //恢复初始状态 priceOriginator.RecoveryState(priceCaretaker.Memeto); //priceInfo接收定价信息 PriceModel priceInfo = priceOriginator.Result(); //返回定价信息 return priceInfo; }