You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

2 years ago
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<IMemento> _mementos = new List<IMemento>();
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());
}
}
}
}