├── MementoDesignPattern.sln
├── MementoDesignPattern
├── MementoDesignPattern.csproj
├── Models
│ └── Memento.cs
├── Program.cs
└── Services
│ ├── Caretaker.cs
│ └── Originator.cs
└── README.md
/MementoDesignPattern.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34916.146
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MementoDesignPattern", "MementoDesignPattern\MementoDesignPattern.csproj", "{F07B48CF-8FC8-44C9-ADDD-5ECFE06B7663}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {F07B48CF-8FC8-44C9-ADDD-5ECFE06B7663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {F07B48CF-8FC8-44C9-ADDD-5ECFE06B7663}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {F07B48CF-8FC8-44C9-ADDD-5ECFE06B7663}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {F07B48CF-8FC8-44C9-ADDD-5ECFE06B7663}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {9BF5ECB0-FDA6-49D1-A8FB-098F890DDD1F}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/MementoDesignPattern/MementoDesignPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/MementoDesignPattern/Models/Memento.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace MementoDesignPattern.Models
8 | {
9 | public class Memento
10 | {
11 | public T State { get; private set; }
12 |
13 | public Memento(T state)
14 | {
15 | State = state;
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/MementoDesignPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using MementoDesignPattern.Services;
2 |
3 | public class Program
4 | {
5 | public static void Main(string[] args)
6 | {
7 | var originator = new Originator();
8 | var caretaker = new Caretaker(originator);
9 |
10 | originator.State = "State 1";
11 | caretaker.Backup();
12 |
13 | originator.State = "State 2";
14 | caretaker.Backup();
15 |
16 | originator.State = "State 3";
17 |
18 | Console.WriteLine("Current State: " + originator.State);
19 | caretaker.Undo();
20 | Console.WriteLine("After Undo: " + originator.State);
21 | caretaker.Undo();
22 | Console.WriteLine("After Second Undo: " + originator.State);
23 | caretaker.Redo();
24 | Console.WriteLine("After Redo: " + originator.State);
25 | }
26 | }
--------------------------------------------------------------------------------
/MementoDesignPattern/Services/Caretaker.cs:
--------------------------------------------------------------------------------
1 | using MementoDesignPattern.Models;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace MementoDesignPattern.Services
9 | {
10 | public class Caretaker
11 | {
12 | private readonly List> _undoMementos = new List>();
13 | private readonly List> _redoMementos = new List>();
14 | private readonly Originator _originator;
15 |
16 | public Caretaker(Originator originator)
17 | {
18 | _originator = originator;
19 | }
20 |
21 | public void Backup()
22 | {
23 | _undoMementos.Add(_originator.SaveState());
24 | _redoMementos.Clear();
25 | }
26 |
27 | public void Undo()
28 | {
29 | if (_undoMementos.Count == 0) return;
30 |
31 | var memento = _undoMementos[^1];
32 | _undoMementos.RemoveAt(_undoMementos.Count - 1);
33 | _redoMementos.Add(_originator.SaveState());
34 | _originator.RestoreState(memento);
35 | }
36 |
37 | public void Redo()
38 | {
39 | if (_redoMementos.Count == 0) return;
40 |
41 | var memento = _redoMementos[^1];
42 | _redoMementos.RemoveAt(_redoMementos.Count - 1);
43 | _undoMementos.Add(_originator.SaveState());
44 | _originator.RestoreState(memento);
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/MementoDesignPattern/Services/Originator.cs:
--------------------------------------------------------------------------------
1 | using MementoDesignPattern.Models;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace MementoDesignPattern.Services
9 | {
10 | public class Originator
11 | {
12 | public T State { get; set; }
13 |
14 | public Memento SaveState()
15 | {
16 | return new Memento(State);
17 | }
18 |
19 | public void RestoreState(Memento memento)
20 | {
21 | State = memento.State;
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | ---
3 |
4 | # MementoDesignPattern
5 |
6 | ### Memento Design Pattern in C#
7 |
8 | #### Description:
9 | This project demonstrates the implementation of the Memento Design Pattern in C#. The Memento Pattern allows saving and restoring an object's state, enabling undo and redo functionalities. This is useful in applications that require state management like text editors, graphics editors, etc.
10 |
11 | #### Features:
12 | - **Dynamic State Storage**: Uses generics to store any type of state.
13 | - **State Management**: Save and restore the state efficiently.
14 | - **Undo and Redo Functionality**: Supports multiple levels of undo and redo.
15 | - **Clear Separation of Concerns**: Organized into distinct classes and namespaces.
16 |
17 | #### Classes:
18 | - **Memento**: Stores the state of an object.
19 | - **Originator**: The object whose state is saved and restored.
20 | - **Caretaker**: Manages the saving and restoring of the Originator's state using Mementos.
21 |
22 | #### Usage:
23 | ```csharp
24 | var originator = new Originator();
25 | var caretaker = new Caretaker(originator);
26 |
27 | originator.State = "State 1";
28 | caretaker.Backup();
29 |
30 | originator.State = "State 2";
31 | caretaker.Backup();
32 |
33 | originator.State = "State 3";
34 |
35 | Console.WriteLine("Current State: " + originator.State);
36 | caretaker.Undo();
37 | Console.WriteLine("After Undo: " + originator.State);
38 | caretaker.Undo();
39 | Console.WriteLine("After Second Undo: " + originator.State);
40 | caretaker.Redo();
41 | Console.WriteLine("After Redo: " + originator.State);
42 | ```
43 |
44 | ---
45 |
46 | This project serves as a template for applications requiring dynamic and flexible state management using the Memento Design Pattern.
47 |
48 | ---
49 |
50 | Feel free to customize this description according to your project's specifics and requirements.
51 |
52 | ---
53 |
54 |
--------------------------------------------------------------------------------