Add code examples, and documentation
This commit is contained in:
46
patterns/memento/MementoPattern/Originator.cs
Normal file
46
patterns/memento/MementoPattern/Originator.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user