I. Definition of the memorandum pattern:
captures the internal state of an object without breaking the closure, and saves the state outside of the object. The object can then be restored to its previously saved state.
Two. Structure and role of the memo pattern:
1.Originator (Initiator): Responsible for creating a memo memento that records the internal state of the current moment itself, and can use the memo to restore the internal state. Originator can determine what internal state the memento stores itself as needed.
2.Memento (Memo): is responsible for storing the internal state of the originator object, and prevents access to the memo from objects other than originator. The memo has two interfaces: caretaker can only see the narrow interface of the memo, and he can only pass the memo to other objects. Originator can see the wide interface of the memo, allowing it to access all the data needed to return to the previous state.
3.Caretaker (Manager): Responsible for memo memento, cannot access or operate the content of memento.
Three. The implementation of the memo pattern:
The example of backup phone address Book is implemented.
<summary>///contact person/// </summary> public class ContactPerson {public string name { Get Set Public string Tel {get; set;} }
//< Summary>///Memo///</summary> public class Memento {//Save initiator's internal state public List<con Tactperson> Contactpersonback; Public Memento (list<contactperson> persons) {this.contactpersonback = persons; } }
<summary>//Initiator///</summary> public class Originator {//internal state public Lis t<contactperson> person; Public originator (list<contactperson> person) {This.person = person; }//<summary>//Create Memo///</summary>//<returns></returns> Public Memento Creatememento () {//here should also pass a deep copy, the new list way is a shallow copy,//Because the ContactPerson class is all str ing type, so here the new list method performs a deep copy of the ContactPerson object//If ContactPerson includes a non-string reference type, there is a problem, so a deep copy of the re should also be passed with serialization Turn New Memento (New List<contactperson> (This.person)); }///<summary>//Import the data backup in the memo into the contact list///</summary>//<param name= "Memento "></param> public void Restorememento (Memento Memento) {if (Memento! = null) {//The following is a wrong way to pass a reference,/The deletion can be resumed once, but it will not recover if it is deleted after recovery. So a deep copy of Contactpersonback should be passed, and a deep copy can be serialized to complete This.person = Memento.contactpersonback; }}///<summary>//Display information///</summary> public void Gethow () { Console.WriteLine ("There are {0} individuals in the contact list, they are:", person.) Count); for (int i = 0; i < person. Count; i++) {Console.WriteLine ("Name: {0} number: {1}", Person[i].name, Person[i].tel); } } }
//< Summary>///Manager///</summary> public class Caretaker {//Use multiple memos to store multiple backup points public Dict Ionary<string, memento> contactmementodic {get; set;} Public caretaker () {contactmementodic = new dictionary<string, memento> (); } }
<summary>///C # design mode-Memo mode///</summary> class Program {static void Main (string[] args) {int num = 0; list<contactperson> persons = new List<contactperson> () {new ContactPerson () {Name = "Zhang San", tel = "111111111"}, new ContactPerson () {name = "John Doe", tel = "000000000"}, New Cont Actperson () {name = "Harry", tel = "333333333"}}; Console.WriteLine ("section" {0} "phase", num); Originator admin = new originator (persons); Admin. Gethow (); Create a memo and save the memo object caretaker caretaker = new Caretaker (); Caretaker. Contactmementodic.add (Num. ToString (), admin. Creatememento ()); Change the initiator contact list num + = 1; Console.WriteLine ("section" {0} "phase", num); Console.WriteLine ("----Remove last contact--------"); Admin.person.RemoveAt (2); Admin. Gethow (); Create a second backup Thread.Sleep (1000); Caretaker. Contactmementodic.add (Num. ToString (), admin. Creatememento ()); Revert to default state var KeyCollection = caretaker. Contactmementodic.keys; while (true) {Console.Write ("Enter a number indicating the stage to roll back:"); int index =-1; try {index = Int32.Parse (Console.ReadLine ()); } catch {Console.WriteLine ("Malformed input"); Continue } Memento contactmentor = null; if (Index < KeyCollection. Count && caretaker. Contactmementodic.trygetvalue (KeyCollection. ElementAt (index), out Contactmentor)) {Admin. Restorememento (Contactmentor); Admin. Gethow (); } else {Console.WriteLine ("The input index is greater than the set length! "); } } } }
Four. Advantages and disadvantages of Memo mode:
Advantages:
1, sometimes the internal information of the initiator object must be kept outside the initiator object, but must be read by the initiator object itself, the use of Memo mode can be complex initiator internal information to the other objects can be shielded, so as to properly maintain the boundaries of encapsulation.
2. This model simplifies the initiation of human beings. The initiator no longer needs to manage and save a version of its internal state, and the client can manage the versions of those states they need.
3, when the status of the initiator role changes, it is possible that the state is invalid, this time you can use the temporarily stored memo to restore the state.
Disadvantages:
1. If the status of the initiator role needs to be stored completely in the memo object, the memo object will be expensive on resource consumption.
2. When the responsible person stores a memo, the person in charge may not know how much storage space the state occupies, and thus cannot remind the user whether an operation is expensive.
3, when the status of the initiator role changes, it is possible that this agreement is invalid. If the success rate of state change is not high, it is better to take the "if" protocol mode.
"C # design mode-Memo mode"