Skip to content

Commit a0914b1

Browse files
committed
memnto pattern example
1 parent 3fe8d0c commit a0914b1

File tree

3 files changed

+107
-8
lines changed

3 files changed

+107
-8
lines changed

DesignPatterns.sln

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BridgePattern", "BridgePatt
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompositePattern", "CompositePattern\CompositePattern.csproj", "{3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}"
2121
EndProject
22-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FacadePattern", "FacadePattern\FacadePattern.csproj", "{8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}"
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FacadePattern", "FacadePattern\FacadePattern.csproj", "{8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}"
2323
EndProject
24-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DecoratorPattern", "DecoratorPattern\DecoratorPattern.csproj", "{E16867D5-A235-437D-ADBF-8A4418E7038B}"
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DecoratorPattern", "DecoratorPattern\DecoratorPattern.csproj", "{E16867D5-A235-437D-ADBF-8A4418E7038B}"
2525
EndProject
26-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlyweightPattern", "FlyweightPattern\FlyweightPattern.csproj", "{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}"
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlyweightPattern", "FlyweightPattern\FlyweightPattern.csproj", "{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}"
2727
EndProject
28-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}"
28+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}"
2929
EndProject
30-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.csproj", "{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}"
30+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.csproj", "{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}"
3131
EndProject
32-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandPattern", "CommandPattern\CommandPattern.csproj", "{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}"
32+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandPattern", "CommandPattern\CommandPattern.csproj", "{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}"
3333
EndProject
34-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{BB1AE146-315A-44F8-A90B-77E386C65678}"
34+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{BB1AE146-315A-44F8-A90B-77E386C65678}"
3535
EndProject
36-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediatorPattern", "MediatorPattern\MediatorPattern.csproj", "{A6278444-3DDF-4175-996C-361E536668FC}"
36+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatorPattern", "MediatorPattern\MediatorPattern.csproj", "{A6278444-3DDF-4175-996C-361E536668FC}"
37+
EndProject
38+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MementoPattern", "Memento\MementoPattern.csproj", "{93F0318A-696F-4C09-800F-7091BAEC2178}"
3739
EndProject
3840
Global
3941
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -105,6 +107,10 @@ Global
105107
{A6278444-3DDF-4175-996C-361E536668FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
106108
{A6278444-3DDF-4175-996C-361E536668FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
107109
{A6278444-3DDF-4175-996C-361E536668FC}.Release|Any CPU.Build.0 = Release|Any CPU
110+
{93F0318A-696F-4C09-800F-7091BAEC2178}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111+
{93F0318A-696F-4C09-800F-7091BAEC2178}.Debug|Any CPU.Build.0 = Debug|Any CPU
112+
{93F0318A-696F-4C09-800F-7091BAEC2178}.Release|Any CPU.ActiveCfg = Release|Any CPU
113+
{93F0318A-696F-4C09-800F-7091BAEC2178}.Release|Any CPU.Build.0 = Release|Any CPU
108114
EndGlobalSection
109115
GlobalSection(SolutionProperties) = preSolution
110116
HideSolutionNode = FALSE

Memento/MementoPattern.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

Memento/Program.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
3+
namespace MementoPattern
4+
{
5+
class EditorMemento
6+
{
7+
private string mContent;
8+
9+
public EditorMemento(string content)
10+
{
11+
mContent = content;
12+
}
13+
14+
public string Content
15+
{
16+
get
17+
{
18+
return mContent;
19+
}
20+
}
21+
}
22+
23+
class Editor {
24+
25+
private string mContent = string.Empty;
26+
private EditorMemento memento;
27+
28+
public Editor()
29+
{
30+
memento = new EditorMemento(string.Empty);
31+
}
32+
33+
public void Type(string words)
34+
{
35+
mContent = String.Concat(mContent," ", words);
36+
}
37+
38+
public string Content
39+
{
40+
get
41+
{
42+
return mContent;
43+
}
44+
}
45+
46+
public void Save()
47+
{
48+
memento = new EditorMemento(mContent);
49+
}
50+
51+
public void Restore()
52+
{
53+
mContent = memento.Content;
54+
}
55+
}
56+
57+
58+
class Program
59+
{
60+
static void Main(string[] args)
61+
{
62+
var editor = new Editor();
63+
64+
//Type some stuff
65+
editor.Type("This is the first sentence.");
66+
editor.Type("This is second.");
67+
68+
// Save the state to restore to : This is the first sentence. This is second.
69+
editor.Save();
70+
71+
//Type some more
72+
editor.Type("This is thrid.");
73+
74+
//Output the content
75+
Console.WriteLine(editor.Content); // This is the first sentence. This is second. This is third.
76+
77+
//Restoring to last saved state
78+
editor.Restore();
79+
80+
Console.Write(editor.Content); // This is the first sentence. This is second
81+
82+
Console.ReadLine();
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)