Link copied to clipboard!
Memento
Capture and externalize state; restore later.
C#
// Memento object
class Snapshot
{
public string State { get; }
public Snapshot(string s) => State = s;
}
// Originator
class Editor
{
private string _text = string.Empty;
public void Type(string s) => _text += s;
public Snapshot Save() => new Snapshot(_text);
public void Restore(Snapshot m) => _text = m.State;
public string Text() => _text;
}
// Usage
var ed = new Editor();
ed.Type("ab");
var snap = ed.Save();
ed.Type("c");
ed.Restore(snap); // back to "ab"
next