版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、设计模式(Design Pattern),张凯 副教授 计算机学院 软件工程系,问题(Problem),问题(Problem),问题(Problem),问题(Problem),问题(Problem),问题(Problem),我要保存游戏进度 游戏角色,生命力、攻击力、防御力 模拟战斗代码,读取进度,问题(Problem),class GameRole /生命力 private int vit; public int Vitality get return vit; set vit = value; /攻击力 private int atk; public int Attack get retu
2、rn atk; set atk = value; /防御力 private int def; public int Defense get return def; set def = value; ,/状态显示 public void StateDisplay() Console.WriteLine(角色当前状态:); Console.WriteLine(体力:0, this.vit); Console.WriteLine(攻击力:0, this.atk); Console.WriteLine(防御力:0, this.def); Console.WriteLine(); /获得初始状态 pub
3、lic void GetInitState() this.vit = 100; this.atk = 100; this.def = 100; /战斗 public void Fight() this.vit = 0; this.atk = 0; this.def = 0; ,问题(Problem),static void Main(string args) /大战Boss前 GameRole lixiaoyao = new GameRole(); lixiaoyao.GetInitState(); lixiaoyao.StateDisplay(); /保存进度 GameRole backup
4、 = new GameRole(); backup.Vitality = lixiaoyao.Vitality; backup.Attack = lixiaoyao.Attack; backup.Defense = lixiaoyao.Defense; /大战Boss时,损耗严重 lixiaoyao.Fight(); lixiaoyao.StateDisplay(); /恢复之前状态 lixiaoyao.Vitality = backup.Vitality; lixiaoyao.Attack = backup.Attack; lixiaoyao.Defense = backup.Defense
5、; lixiaoyao.StateDisplay(); Console.Read(); ,主要内容,备忘录模式(Memento),模式动机 模式名称:备忘录模式(Memento) 在应用软件的开发过程中,有时我们有必要记录一个对象的内部状态。为了允许用户取消不确定的操作或从错误中恢复过来,需要实现备份点和撤销机制,而要实现这些机制,我们必须事先将状态信息保存在某处,这样状态才能将对象恢复到它们原先的状态。,备忘录模式(Memento),模式动机 但是对象通常封装了其部分或所有的状态信息,使得其状态不能被其它对象访问,也就不可能在该对象之外保存其状态,而暴露其内部状态又将违反封装的原则,可能有损
6、系统的可靠性和可扩展性。,备忘录模式(Memento),模式定义 备忘录模式(Memento Pattern):在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。,备忘录模式(Memento),备忘录模式(Memento),class GameRole /保存角色状态 public RoleStateMemento SaveState() return (new RoleStateMemento(vit, atk, def); /恢复角色状态 public void RecoveryState(RoleStateMemento
7、memento) this.vit = memento.Vitality; this.atk = memento.Attack; this.def = memento.Defense; ,备忘录模式(Memento),class RoleStateMemento /角色状态存储箱 private int vit; private int atk; private int def; public RoleStateMemento(int vit, int atk, int def) this.vit = vit; this.atk = atk; this.def = def; public in
8、t Vitality /生命力 get return vit; set vit = value; public int Attack /攻击力 get return atk; set atk = value; public int Defense /防御力 get return def; set def = value; ,备忘录模式(Memento),/角色状态管理者 class RoleStateCaretaker private RoleStateMemento memento; public void ImportState(RoleStateMemento Record) memen
9、to = Record; public RoleStateMemento ExportState() return memento; ,备忘录模式(Memento),static void Main(string args) /大战Boss前 GameRole lixiaoyao = new GameRole(); lixiaoyao.GetInitState(); lixiaoyao.StateDisplay(); /保存进度 RoleStateCaretaker stateAdmin = new RoleStateCaretaker(); stateAdmin.ImportState(li
10、xiaoyao.SaveState(); /大战Boss时,损耗严重 lixiaoyao.Fight(); lixiaoyao.StateDisplay(); /恢复之前状态 lixiaoyao.RecoveryState(stateAdmin.ExportState(); lixiaoyao.StateDisplay(); Console.Read(); ,备忘录模式(Memento),模式结构,备忘录模式(Memento),参与者 Originator:原发器 Memento:备忘录 Caretaker:负责人,备忘录模式(Memento),public class Originator
11、private string _state; public string State get return _state; set _state = value; / 创建备忘录,将当前要保存的信息导入并实例化备忘录 public Memento CreateMemento() return (new Memento(this.State); / 恢复备忘录,将Memento导入并将相关数据恢复 public void SetMemento(Memento memento) this.State = memento.State; / 展示状态数据 public void Show() Cons
12、ole.WriteLine(当前状态是: + this.State); ,备忘录模式(Memento),public class Memento private string _state; public string State get return _state; set _state = value; public Memento(string state) this.State = state; ,备忘录模式(Memento),public class Caretaker private Memento _memento; public Memento Memento get retu
13、rn _memento; set _memento = value; ,备忘录模式(Memento),static void Main(string args) Originator o = new Originator(); /初始状态为On o.State = On; o.Show(); /创建备忘录并保存状态 Caretaker caretaker = new Caretaker(); caretaker.Memento = o.CreateMemento(); /更改Originator状态=Off o.State = Off; o.Show(); /恢复到原始状态 o.SetMeme
14、nto(caretaker.Memento); o.Show(); Console.ReadKey(); ,备忘录模式(Memento),电脑销售代理备忘录,备忘录模式(Memento),备忘录模式(Memento),class SalesProspect private string _name; private string _phone; private double _budget; public string Name get return _name; set _name = value; Console.WriteLine(Name: + _name); public strin
15、g Phone get return _phone; set _phone = value; Console.WriteLine(Phone: + _phone); public double Budget get return _budget; set _budget = value; Console.WriteLine(Budget: + _budget); public Memento SaveMemento() Console.WriteLine(nSaving state -n); return new Memento(_name, _phone, _budget); public
16、void RestoreMemento(Memento memento) Console.WriteLine(nRestoring state -n); this.Name = memento.Name; this.Phone = memento.Phone; this.Budget = memento.Budget; ,备忘录模式(Memento),class Memento private string _name; private string _phone; private double _budget; public Memento(string name, string phone
17、, double budget) this._name = name; this._phone = phone; this._budget = budget; public string Name get return _name; set _name = value; public string Phone get return _phone; set _phone = value; public double Budget get return _budget; set _budget = value; ,备忘录模式(Memento),class ProspectMemory privat
18、e Memento _memento; / Property public Memento Memento set _memento = value; get return _memento; ,备忘录模式(Memento),static void Main(string args) SalesProspect s = new SalesProspect(); s.Name = 张三; s.Phone = (027) 82560990; s.Budget = 25000.0; / Store internal state ProspectMemory m = new ProspectMemory(); m.Memento = s.SaveMemento(); s.Name = 李四; s.Phone = (027) 82097111; s.Budget = 10
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 学生自我认知鉴定总结
- 还建房购房合同书(3篇)
- 合同岗位责任制度
- 15《我的一天》(教学设计)2023-2024学年美术四年级上册岭南版
- 幼儿园小班环境布置设计方案(35篇)
- 编辑部年度总结5篇
- 合同样例网络销售代运营合同 范本
- 江苏省泰州市兴化市2024年中考押题数学预测卷含解析
- 毫米和分米的认识(教案)-二年级下册数学青岛版(五四制)
- 江苏省射阳县重点名校2024年中考试题猜想数学试卷含解析
- 2023年中级银行从业资格之中级公司信贷真题练习试卷A卷附答案
- 2024-2034年中国超高强度抽油杆行业市场现状分析及竞争格局与投资发展研究报告
- 软件项目售后服务方案
- 人教版小学四年级下册数学期末测试卷含完整答案【历年真题】
- 【执行手册】2014羽林争霸.ppt
- 预埋件计算表格excel
- 新概念2AUnit6
- 余秋雨,《关于友情》解读.ppt
- 小班语言活动《五颜六色的小老鼠》PPT课件
- 层次分析法练习参考答案
- 供应商管理程序.doc
评论
0/150
提交评论