行为型模式-备忘录memento
来源:互联网 发布:淘宝商城质检报告 编辑:程序博客网 时间:2024/05/19 21:04
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。
using System;using System.Collections.Generic;using System.Text;namespace 备忘录模式{ class Program { static void Main(string[] args) { //大战Boss前 GameRole lixiaoyao = new GameRole(); lixiaoyao.GetInitState(); lixiaoyao.StateDisplay(); //保存进度 RoleStateCaretaker stateAdmin = new RoleStateCaretaker(); stateAdmin.Memento = lixiaoyao.SaveState(); //大战Boss时,损耗严重 lixiaoyao.Fight(); lixiaoyao.StateDisplay(); //恢复之前状态 lixiaoyao.RecoveryState(stateAdmin.Memento); lixiaoyao.StateDisplay(); Console.Read(); } } class GameRole { //生命力 private int vit; public int Vitality { get { return vit; } set { vit = value; } } //攻击力 private int atk; public int Attack { get { return 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(""); } //保存角色状态 public RoleStateMemento SaveState() { return (new RoleStateMemento(vit, atk, def)); } //恢复角色状态 public void RecoveryState(RoleStateMemento memento) { this.vit = memento.Vitality; this.atk = memento.Attack; this.def = memento.Defense; } //获得初始状态 public void GetInitState() { this.vit = 100; this.atk = 100; this.def = 100; } //战斗 public void Fight() { this.vit = 0; this.atk = 0; this.def = 0; } } //角色状态存储箱 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 int 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; } } } //角色状态管理者 class RoleStateCaretaker { private RoleStateMemento memento; public RoleStateMemento Memento { get { return memento; } set { memento = value; } } }}
0 0
- 行为型模式-备忘录memento
- 行为型模式-----备忘录模式(Memento)
- [设计模式-行为型]备忘录模式(Memento)
- 行为型模式-Memento ( 备忘录模式 )
- Memento 备忘录模式 ----对象行为型模式
- 十八、备忘录模式Memento(行为型)
- 行为型模式之备忘录MEMENTO
- 备忘录模式Memento(对象行为型)
- 行为模式:Memento(备忘录)
- 设计模式笔记--行为型模式之六--Memento 备忘录
- 设计模式(18)-行为型-备忘录模式(Memento)
- 行为型模式之备忘录模式(Memento)
- 设计模式--备忘录模式Memento(行为型)
- 设计模式(行为型)之备忘录模式(Memento Pattern)
- JAVA设计模式(19) —<行为型>备忘录模式(Memento)
- 七、备忘录模式Memento(行为型模式)
- 设计模式(4)-行为型-备忘录模式(Memento)
- 设计模式-行为型- 备忘录模式(Memento)
- 行为型模式-迭代器interator
- Java反射:边看书边总结
- swift基础(三)函数定义
- 即使被拖库,也可以保证密码不泄露
- 行为型模式-中介者mediator
- 行为型模式-备忘录memento
- operator new, new operator, delete new, new delete
- 行为型模式-观察者observer
- 解决Codeforces访问慢的本地方法
- PPAPI插件的绘图与输入事件处理
- IOS-IOS应用的核心(一)
- 【杭电oj】1215 - 七夕节(打表,水)
- 归档,解档
- C++中预处理(条件编译)