Skip to content

Commit 54b57fe

Browse files
committed
add Momento pattern
1 parent 394350d commit 54b57fe

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

Behavioral/Memento/CareTaker.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Memento {
5+
6+
class CareTaker {
7+
8+
static void Main(string[] args) {
9+
IList<Originator.Momento> savedStates = new List<Originator.Momento>();
10+
11+
var originator = new Originator();
12+
originator.SetState("State1");
13+
originator.SetState("State2");
14+
savedStates.Add(originator.SaveToMomento());
15+
originator.SetState("State3");
16+
savedStates.Add(originator.SaveToMomento());
17+
originator.SetState("State4");
18+
originator.RestoreFromMomento(savedStates[0]);
19+
20+
Console.ReadKey();
21+
}
22+
}
23+
}

Behavioral/Memento/Memento.csproj

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{C404876C-B04B-451F-B159-2C0CF8CB861B}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Memento</RootNamespace>
12+
<AssemblyName>Memento</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<PropertyGroup>
37+
<StartupObject>Memento.CareTaker</StartupObject>
38+
</PropertyGroup>
39+
<ItemGroup>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="CareTaker.cs" />
50+
<Compile Include="Originator.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55+
Other similar extension points exist, see Microsoft.Common.targets.
56+
<Target Name="BeforeBuild">
57+
</Target>
58+
<Target Name="AfterBuild">
59+
</Target>
60+
-->
61+
</Project>

Behavioral/Memento/Originator.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
3+
namespace Memento {
4+
5+
public class Originator {
6+
7+
private string _state;
8+
9+
public void SetState(string state) {
10+
Console.WriteLine("Originator: setting momento to {0}", state);
11+
this._state = state;
12+
}
13+
14+
public Momento SaveToMomento() {
15+
Console.WriteLine("Originator: saving to momento...");
16+
return new Originator.Momento(this._state);
17+
}
18+
19+
public void RestoreFromMomento(Momento momento) {
20+
this._state = momento.State;
21+
Console.WriteLine("Originator: resotring from momento {0}", this._state);
22+
}
23+
24+
public class Momento {
25+
26+
private readonly string _state;
27+
28+
internal Momento(string state) {
29+
this._state = state;
30+
}
31+
32+
public string State {
33+
get {
34+
return this._state;
35+
}
36+
}
37+
}
38+
}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Memento")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Memento")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("02b2940c-887c-4401-9b58-88a108d729b8")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

DesignPatterns.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iterator", "Behavioral\Iter
1313
EndProject
1414
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator", "Behavioral\Mediator\Mediator.csproj", "{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}"
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memento", "Behavioral\Memento\Memento.csproj", "{C404876C-B04B-451F-B159-2C0CF8CB861B}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -53,6 +55,12 @@ Global
5355
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Release|Any CPU.ActiveCfg = Release|x86
5456
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Release|x86.ActiveCfg = Release|x86
5557
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Release|x86.Build.0 = Release|x86
58+
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Debug|Any CPU.ActiveCfg = Debug|x86
59+
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Debug|x86.ActiveCfg = Debug|x86
60+
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Debug|x86.Build.0 = Debug|x86
61+
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Release|Any CPU.ActiveCfg = Release|x86
62+
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Release|x86.ActiveCfg = Release|x86
63+
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Release|x86.Build.0 = Release|x86
5664
EndGlobalSection
5765
GlobalSection(SolutionProperties) = preSolution
5866
HideSolutionNode = FALSE
@@ -63,5 +71,6 @@ Global
6371
{6007D4E1-1676-4FBC-816D-1B19F6829F22} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
6472
{2A37AC43-5F17-4774-B9BE-8D5008F41F43} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
6573
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
74+
{C404876C-B04B-451F-B159-2C0CF8CB861B} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
6675
EndGlobalSection
6776
EndGlobal

0 commit comments

Comments
 (0)