设计模式之备忘录模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式之备忘录模式相关的知识,希望对你有一定的参考价值。

组成

  • Originator(发起者):需要对状态进行保存和读取的成员
  • Memotor(备忘录):备忘录对象中包含存储发起人状态的成员变量
  • Caretaker(备忘录管理者):用于管理备忘录对象的实现类

类图

技术分享

 

实例类图

技术分享

代码

技术分享
  1 /// <summary>
  2     /// 游戏场景
  3     /// </summary>
  4     class GameSence : IShow
  5     {
  6         public Hero GameHero { get; set; }
  7         public Monster GameMonster { get; set; }
  8 
  9         public void LoadMemento(GameMemento memento)
 10         {
 11             GameHero = memento.MementoHero;
 12             GameMonster = memento.MementoMonster;
 13         }
 14 
 15         public GameMemento SaveMemento()
 16         {
 17             return new GameMemento(GameHero, GameMonster);
 18         }
 19 
 20         public void AttackMonster()
 21         {
 22             GameHero.AttackMonster(GameMonster);
 23         }
 24 
 25         public void Show()
 26         {
 27             IShow show = GameHero;
 28             show.Show();
 29             show = GameMonster;
 30             show.Show();
 31         }
 32     }
 33 
 34   /// <summary>
 35     /// 怪物
 36     /// </summary>
 37     class Monster : IShow, ICloneable
 38     {
 39         private int _hp;
 40         public int HP
 41         {
 42             get
 43             {
 44                 return _hp >= 0 ? _hp : 0;
 45             }
 46             set
 47             {
 48                 _hp = value;
 49                 if (_hp <= 0)
 50                 {
 51                     Console.WriteLine("Monster Dead");
 52                     _hp = 0;
 53                 }
 54             }
 55         }
 56 
 57         #region IShow
 58 
 59         public void Show()
 60         {
 61             Console.WriteLine("Monster HP : " + HP);
 62         }
 63 
 64         #endregion
 65 
 66         public object Clone()
 67         {
 68             return MemberwiseClone();
 69         }
 70     }
 71 
 72 /// <summary>
 73     /// 角色
 74     /// </summary>
 75     class Hero : IShow, ICloneable
 76     {
 77         private int _hp;
 78         public int HP
 79         {
 80             get { return _hp; }
 81             set { _hp = value; }
 82         }
 83 
 84         private int _mp;
 85         public int MP
 86         {
 87             get { return _mp; }
 88             set
 89             {
 90                 _mp = value;
 91             }
 92         }
 93 
 94         public int Attack { get; set; }
 95 
 96         public void AttackMonster(Monster monster)
 97         {
 98             if (MP > 0)
 99             {
100                 Console.WriteLine("Attack : " + Attack);
101                 monster.HP = monster.HP - Attack;
102                 MP--;
103             }
104             else
105             {
106                 Console.WriteLine("No Enough MP");
107             }
108         }
109 
110         #region IShow
111         public void Show()
112         {
113             var info = string.Format("HP : {0} | MP : {1} | Attact : {2}", HP, MP, Attack);
114             Console.WriteLine("Hero " + info);
115         }
116         #endregion
117 
118         public object Clone()
119         {
120             return MemberwiseClone();
121         }
122     }
123 
124    /// <summary>
125     /// 游戏存档
126     /// </summary>
127     class GameMemento
128     {
129         public Hero MementoHero { get; set; }
130         public Monster MementoMonster { get; set; }
131 
132         public GameMemento(Hero hero, Monster monster)
133         {
134             MementoHero = hero.Clone() as Hero;
135             MementoMonster = monster.Clone() as Monster;
136         }
137     }
138 
139  /// <summary>
140     /// 存档管理类
141     /// </summary>
142     internal class Caretaker
143     {
144         private GameMemento _memento;
145 
146         public void SetMemento(GameMemento memento)
147         {
148             ConsoleHelper.WriteImportString("Save Memento");
149             _memento = memento;
150         }
151 
152         public GameMemento GetMemento()
153         {
154             ConsoleHelper.WriteImportString("Load Memento");
155             return _memento;
156         }
157     }
业务代码
技术分享
 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             GameSence game = new GameSence();
 6 
 7             var hero = new Hero
 8             {
 9                 Attack = 5,
10                 HP = 100,
11                 MP = 100
12             };
13             game.GameHero = hero;
14 
15             var monster = new Monster { HP = 400 };
16             game.GameMonster = monster;
17 
18             game.Show();
19             game.AttackMonster();
20             game.Show();
21             game.AttackMonster();
22             game.Show();
23             var caretaker = new Caretaker();
24             caretaker.SetMemento(game.SaveMemento());
25             game.AttackMonster();
26             game.Show();
27             game.LoadMemento(caretaker.GetMemento());
28             game.Show();
29 
30             Console.ReadLine();
31         }
32     }
客户端代码

运行结果
技术分享

 

优点:待补充

缺点:待补充

 

以上是关于设计模式之备忘录模式的主要内容,如果未能解决你的问题,请参考以下文章

折腾Java设计模式之备忘录模式

设计模式之备忘录模式

GoF 23 种设计模式之状态模式和备忘录模式

设计模式之备忘录模式

设计模式之备忘录

设计模式之状态模式与备忘录模式详解和应用