备忘就是为了恢复数据嘛, 电脑系统知道什么需要保存什么不需要保存,文件夹只提供保存的位置,不关心具体的保存行为。
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1 { class Program { public class Memento { private string status; public string Status { get { return status; } set { Console.WriteLine("status is " + status); status = value; } } public Memento(string status) { this.status = status; } } public class MicrosoftSystem { private string status; public string Status { get { return status; } set { status = value; } } public Memento CreateMemento() { return new Memento(status); } public void RecoverSystem(Memento m) { this.status = m.Status; } } public class Folder { private Memento memento; public Memento RecoverMememto() { return this.memento; } public void SaveMemento(Memento memento) { this.memento = memento; } } static void Main(string[] args) { MicrosoftSystem winXp = new MicrosoftSystem(); Folder folder = new Folder(); winXp.Status = "hehe"; folder.SaveMemento(winXp.CreateMemento()); winXp.Status = "haha"; winXp.RecoverSystem(folder.RecoverMememto()); Console.WriteLine(winXp.Status); } } }