备忘录模式是,缓存动作,把上一次动作内容记下来放在缓存中
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MementoSample { class Program { static void DisPlay(Pos pos) { Console.WriteLine($"当前在位置:{pos.Name}+{pos.X}+{pos.Y}"); } static void Main(string[] args) { Pos mpos = new Pos("测试位置", 10, 10); Operater mc = new Operater(); DisPlay(mpos); mc.SetMemento(mpos.Save()); mpos.X = 20; DisPlay(mpos); mpos.Restore(mc.GetMemento()); DisPlay(mpos); Console.Read(); } class Pos { public string Name; public int X, Y; public Pos(string name,int x,int y) { this.Name = name; X = x; Y = y; } internal PosMemento Save() { return new PosMemento(Name, X, Y); } internal void Restore(PosMemento pos) { this.Name = pos.Name; X = pos.X; Y = pos.Y; } } class PosMemento { public string Name; public int X, Y; public PosMemento (string name, int x, int y) { this.Name = name; X = x; Y = y; } } class Operater { private PosMemento memento; internal PosMemento GetMemento() { return memento; } internal void SetMemento(PosMemento memento) { this.memento = memento; } } } } ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200903192717294.png#pic_center)