47 lines
953 B
C#
47 lines
953 B
C#
namespace MementoPattern;
|
|
|
|
public class Originator : IOriginator
|
|
{
|
|
private Guid _id;
|
|
|
|
public Originator()
|
|
{
|
|
_id = Guid.NewGuid();
|
|
Console.WriteLine($"Starting ID: {_id}");
|
|
Console.WriteLine();
|
|
}
|
|
|
|
public IMemento GetMemento()
|
|
{
|
|
Memento memento = new Memento();
|
|
memento.Id = _id;
|
|
Console.WriteLine("Creating Memento...");
|
|
Console.WriteLine();
|
|
return memento;
|
|
}
|
|
|
|
public void SetMemento(IMemento memento)
|
|
{
|
|
if (!(memento is Memento))
|
|
return;
|
|
Console.WriteLine("Restting ID...");
|
|
_id = (memento as Memento).Id;
|
|
|
|
Console.WriteLine($"ID is reset to {_id}");
|
|
Console.WriteLine();
|
|
}
|
|
|
|
public void UpdateId()
|
|
{
|
|
Console.WriteLine("Updating Id...");
|
|
_id = Guid.NewGuid();
|
|
Console.WriteLine($"Updated ID is now {_id}");
|
|
Console.WriteLine();
|
|
}
|
|
|
|
private class Memento : IMemento
|
|
{
|
|
public Guid Id;
|
|
}
|
|
}
|