using Memonto.Originators; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Memonto.Mementos { internal class Caretaker { private List _mementos = new List(); private Originator _originator = null; public Caretaker(Originator originator) { _originator = originator; } public void Backup() { Console.WriteLine(); Console.WriteLine("Caretaker: Saving originator's state..."); _mementos.Add(_originator.Save()); } public void Undo() { if (_mementos.Count < 1) return; IMemento memento = _mementos.Last(); _mementos.Remove(memento); Console.WriteLine($"Caretaker: Restoring state to: {memento.GetName()}"); try { _originator.Restore(memento); } catch (Exception ex) { Console.WriteLine($"Undo exception: {ex.Message}"); Undo(); } } public void ShowHistory() { Console.WriteLine("Caretaker: Here's the list of mementos: "); foreach (var memento in _mementos) { Console.WriteLine(memento.GetName()); } } } }