Add code examples, and documentation
This commit is contained in:
24
patterns/memento/MementoPattern/Caretaker.cs
Normal file
24
patterns/memento/MementoPattern/Caretaker.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace MementoPattern;
|
||||
|
||||
public class Caretaker
|
||||
{
|
||||
//container that could hold memento of Originator
|
||||
//object
|
||||
private readonly Stack<IMemento> _mementos = new();
|
||||
private readonly IOriginator _originator;
|
||||
|
||||
public Caretaker(IOriginator originator)
|
||||
{
|
||||
_originator = originator;
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
_mementos.Push(_originator.GetMemento());
|
||||
}
|
||||
|
||||
public void Undo()
|
||||
{
|
||||
_originator.SetMemento(_mementos.Pop());
|
||||
}
|
||||
}
|
||||
3
patterns/memento/MementoPattern/IMemento.cs
Normal file
3
patterns/memento/MementoPattern/IMemento.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace MementoPattern;
|
||||
|
||||
public interface IMemento { }
|
||||
7
patterns/memento/MementoPattern/IOriginator.cs
Normal file
7
patterns/memento/MementoPattern/IOriginator.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace MementoPattern;
|
||||
|
||||
public interface IOriginator
|
||||
{
|
||||
IMemento GetMemento();
|
||||
void SetMemento(IMemento memento);
|
||||
}
|
||||
8
patterns/memento/MementoPattern/MementoPattern.csproj
Normal file
8
patterns/memento/MementoPattern/MementoPattern.csproj
Normal file
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
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;
|
||||
}
|
||||
}
|
||||
45
patterns/memento/MementoPattern/Program.cs
Normal file
45
patterns/memento/MementoPattern/Program.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace MementoPattern;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
Originator originator = new Originator();
|
||||
Caretaker caretaker = new Caretaker(originator);
|
||||
|
||||
caretaker.Save();
|
||||
Console.WriteLine(
|
||||
"After the first save, the originator's first ID is saved as a memento and pushed on the caretaker's stack. Enter any input to continue."
|
||||
);
|
||||
Console.ReadLine();
|
||||
|
||||
originator.UpdateId();
|
||||
originator.UpdateId();
|
||||
|
||||
caretaker.Save();
|
||||
Console.WriteLine(
|
||||
"After two updates and a save, only the second update is saved as a memento and pushed on the caretaker's stack. Enter any input to continue."
|
||||
);
|
||||
Console.ReadLine();
|
||||
|
||||
originator.UpdateId();
|
||||
caretaker.Undo();
|
||||
|
||||
Console.WriteLine(
|
||||
"After this latest update, since we called Undo() the ID will be changed from the latest updated ID to the prior one (i.e. the latest one that was actually saved to the stack). This is the main point of memento. Enter any input to continue."
|
||||
);
|
||||
Console.ReadLine();
|
||||
|
||||
originator.UpdateId();
|
||||
caretaker.Save();
|
||||
|
||||
Console.WriteLine(
|
||||
"A new ID is updated and pushed onto the stack. Enter any input to continue."
|
||||
);
|
||||
Console.ReadLine();
|
||||
|
||||
originator.UpdateId();
|
||||
caretaker.Undo();
|
||||
caretaker.Undo();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user