1、以游戏的场景为例,我们来讲解这个备忘录模式,一游戏角色有生命力、攻击力、防御力等数据,在打Boss后如果效果不理想可以让游戏恢复到决斗前的状态。
2、玩家类
1 class GameRole 2 { 3 //生命力 4 public int Vitality { set; get; } 5 //攻击力 6 public int Attack { set; get; } 7 //防御力 8 public int Defense { set; get; } 9 //状态显示10 public void StateDisplay()11 {12 Console.WriteLine("显示当前状态:");13 Console.WriteLine("体力:{0}",this.Vitality);14 Console.WriteLine("攻击力:{0}",this.Attack);15 Console.WriteLine("防御力: {0}",this.Defense);16 }17 //获得初始状态18 public void GetInitState()19 {20 this.Vitality = 100;21 this.Attack = 100;22 this.Defense = 100;23 }24 //战斗25 public void Fight()26 {27 this.Vitality = 0;28 this.Attack = 0;29 this.Defense = 0;30 }31 }
3、客户端类
1 static void Main(string[] args) 2 { 3 //大战boss前 4 Console.WriteLine("#############_战boss前_#################"); 5 GameRole gr = new GameRole(); 6 gr.GetInitState(); 7 gr.StateDisplay(); 8 9 //通过新的实例来保存进度10 GameRole backup = new GameRole();11 backup.Vitality = gr.Vitality;12 backup.Defense = gr.Defense;13 backup.Attack = gr.Attack;14 15 //战boss后16 Console.WriteLine("#############_战boss后_#################");17 gr.Fight();18 gr.StateDisplay();19 20 //恢复之前的状态21 Console.WriteLine("#############_恢复之前的状态_#################");22 gr.Vitality = backup.Vitality;23 gr.Defense = backup.Defense;24 gr.Attack = backup.Attack;25 gr.StateDisplay();26 27 Console.Read();28 29 30 }
4、这样写问题主要在于客户端的调用,这样把整个游戏角色的细节暴露给了客户端,你的客户端的职责过大,我们希望可以把这些游戏角色的存取状态细节封装起来,而且最好是封装在外部的类当中,以体现职责分离。
5、到这里我们就要用到备忘模式
定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
6、现在我们来改造这个代码,首先要有一个存储状态的类
1 class RoleStateMemento 2 { 3 public int Vitality { set; get; } 4 public int Attack { set; get; } 5 public int Defense { set; get; } 6 public RoleStateMemento(int vit, int att, int def) 7 { 8 this.Vitality = vit; 9 this.Attack = att;10 this.Defense = def;11 12 }13 }
7、改造玩家类
1 class GameRole 2 { 3 //生命力 4 public int Vitality { set; get; } 5 //攻击力 6 public int Attack { set; get; } 7 //防御力 8 public int Defense { set; get; } 9 10 //保存状态11 public RoleStateMemento SaveState()12 {13 return new RoleStateMemento(Vitality, Attack, Defense);14 }15 16 //恢复状态17 public void RecoveryState(RoleStateMemento rs)18 {19 this.Vitality = rs.Vitality;20 this.Attack = rs.Attack;21 this.Defense = rs.Defense;22 }23 //状态显示24 public void StateDisplay()25 {26 Console.WriteLine("显示当前状态:");27 Console.WriteLine("体力:{0}",this.Vitality);28 Console.WriteLine("攻击力:{0}",this.Attack);29 Console.WriteLine("防御力: {0}",this.Defense);30 }31 //获得初始状态32 public void GetInitState()33 {34 this.Vitality = 100;35 this.Attack = 100;36 this.Defense = 100;37 }38 //战斗39 public void Fight()40 {41 this.Vitality = 0;42 this.Attack = 0;43 this.Defense = 0;44 }45 }
8、还要有一个管理角色状态的类
1 class RoleStateCaretaker2 {3 public RoleStateMemento rs;4 }
9、客户端代码
1 static void Main(string[] args) 2 { 3 //大战boss前 4 Console.WriteLine("#############_战boss前_#################"); 5 GameRole gr = new GameRole(); 6 gr.GetInitState(); 7 gr.StateDisplay(); 8 9 //保存进度10 RoleStateCaretaker rc = new RoleStateCaretaker();11 rc.roleState = gr.SaveState();12 13 14 //战boss后15 Console.WriteLine("#############_战boss后_#################");16 gr.Fight();17 gr.StateDisplay();18 19 //恢复之前的状态20 Console.WriteLine("#############_恢复之前的状态_#################");21 gr.RecoveryState(rc.roleState);22 gr.StateDisplay();23 24 Console.Read();25 26 27 }