读书笔记15:备忘录模式

 1、  概念

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复至保存时的状态。

2、  模型

角色:

Originator(发起者)――创建备忘录Memorandum,并且根据需要决定,需要保存那些状态。

Memorandum(备忘录)――负责存储Originator对象的内部状态,并防止Originator以外的对象访问这个备忘录。

Caretaker(管理者)――负责保存备忘录。

程序模型:

//发起者,需要保存状态的对象

 public class Originator      {          private string stateString1;            public string StateString1          {              get { return stateString1; }                set { stateString1 = value; }          }            private string stateString2;            public string StateString2          {              get { return stateString2; }                set { stateString2 = value; }          }              public Memorandum CreateMemorandum()          {              return new Memorandum(stateString1, stateString2);          }            public void SetMemorandum(Memorandum memorandum)          {              StateString1 = memorandum.StateString1;                StateString2 = memorandum.StateString2;          }            public void ShowMemorandum()          {              Console.WriteLine("当前状态:");                Console.WriteLine("StateString1={0}", stateString1, stateString2);                Console.WriteLine("StateString2={0}", stateString1, stateString2);          }      }

// 存储Originator,其中包括的参数和要存的Originator参数一致

 public class Memorandum      {          private string stateString1;            private string stateString2;            public Memorandum(string state1, string state2)          {              this.stateString1 = state1;                this.stateString2 = state2;          }            public string StateString1          {              get { return stateString1; }          }            public string StateString2          {              get { return stateString2; }          }      }

管理者:

 public class CareTaker      {          private Memorandum memorandum;            public Memorandum MemorandumClass          {              get { return memorandum; }                set { memorandum = value; }          }        }

调用:

        static void Main(string[] args)          {              //发起者参数状态初始化              Originator originator = new Originator();                originator.StateString1 = "参数StateString1――初始化1";                originator.StateString2 = "参数StateString2――初始化1";                originator.ShowMemorandum();                //保存一份备份              CareTaker careTaker = new CareTaker();                careTaker.MemorandumClass = originator.CreateMemorandum();                //发起者参数状态改变了              originator.StateString1 = "参数StateString1――初始化2";                originator.StateString2 = "参数StateString2――初始化2";                originator.ShowMemorandum();                //读取保存的进度              originator.SetMemorandum(careTaker.MemorandumClass);                originator.ShowMemorandum();                Console.ReadKey();          }

结果:


   

    备忘录模式,实际就是将一个类的当前状态保存一份放着,当状态改变以后,后面需要的时候再去读取保存的状态。而怎么去保存呢,Memorandum承担了保存参数的状态。如果一个类没有行为,而只有属性的话,那么Memorandum就可以写成和发起者一样的类型,保存的时候就可以保存发起者的实例。

你可能感兴趣的:(String,读书,存储,Class)