static void Main(string[] args) { List <ContactPerson> persons = new List <ContactPerson>() { new ContactPerson() { Name = "Learning Hard", MobileNum = "123445" }, new ContactPerson() { Name = "Tony", MobileNum = "234565" }, new ContactPerson() { Name = "Jock", MobileNum = "231455" } }; MobileOwner mobileOwner = new MobileOwner(persons); mobileOwner.Show(); // 创建备忘录并保存备忘录对象 Caretaker caretaker = new Caretaker(); caretaker.ContactM = mobileOwner.CreateMemento(); // 更改发起人联系人列表 Console.WriteLine("----移除最后一个联系人--------"); mobileOwner.ContactPersons.RemoveAt(2); mobileOwner.Show(); // 恢复到原始状态 Console.WriteLine("-------恢复联系人列表------"); mobileOwner.RestoreMemento(caretaker.ContactM); mobileOwner.Show(); Console.Read(); }
static void Example() { var originator = new Example.Originator("Originator's initial state"); var caretaker = new Example.Caretaker(originator); caretaker.Backup(); originator.DoSomething(); caretaker.Backup(); originator.DoSomething(); caretaker.Backup(); originator.DoSomething(); Console.WriteLine(); caretaker.ShowHistory(); Console.WriteLine("\nClient: Now, let's rollback!\n"); caretaker.Undo(); Console.WriteLine("\n\nClient: Once more!\n"); caretaker.Undo(); }