Add code examples, and documentation

This commit is contained in:
Aaron Po
2026-08-12 00:03:32 -04:00
commit 77c16e69f6
79 changed files with 4458 additions and 0 deletions

View 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());
}
}

View File

@@ -0,0 +1,3 @@
namespace MementoPattern;
public interface IMemento { }

View File

@@ -0,0 +1,7 @@
namespace MementoPattern;
public interface IOriginator
{
IMemento GetMemento();
void SetMemento(IMemento memento);
}

View 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>

View 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;
}
}

View 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();
}
}