Skip to content

Commit f7ba198

Browse files
committed
Add Memento pattern
1 parent b1b23ac commit f7ba198

11 files changed

+326
-16
lines changed

Memento/Memento.csproj

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,22 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45-
<Compile Include="Program.cs" />
45+
<Compile Include="RealWorld\Memento.cs" />
46+
<Compile Include="RealWorld\Program.cs" />
4647
<Compile Include="Properties\AssemblyInfo.cs" />
48+
<Compile Include="RealWorld\ProspectMemory.cs" />
49+
<Compile Include="RealWorld\SalesProspect.cs" />
50+
<Compile Include="Structural\Caretaker.cs" />
51+
<Compile Include="Structural\Memento.cs" />
52+
<Compile Include="Structural\Originator.cs" />
53+
<Compile Include="Structural\Program.cs" />
4754
</ItemGroup>
4855
<ItemGroup>
4956
<None Include="App.config" />
5057
</ItemGroup>
58+
<ItemGroup>
59+
<Content Include="Participants.txt" />
60+
</ItemGroup>
5161
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5262
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5363
Other similar extension points exist, see Microsoft.Common.targets.

Memento/Participants.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Participants:
2+
3+
The classes and objects participating in this pattern are:
4+
5+
Memento (Memento)
6+
stores internal state of the Originator object. The memento may store as much or as little of the originator's
7+
internal state as necessary at its originator's discretion.
8+
protect against access by objects of other than the originator. Mementos have effectively two interfaces.
9+
Caretaker sees a narrow interface to the Memento -- it can only pass the memento to the other objects.
10+
Originator, in contrast, sees a wide interface, one that lets it access all the data necessary to restore
11+
itself to its previous state. Ideally, only the originator that produces the memento would be permitted
12+
to access the memento's internal state.
13+
14+
Originator (SalesProspect)
15+
creates a memento containing a snapshot of its current internal state.
16+
uses the memento to restore its internal state
17+
18+
Caretaker (Caretaker)
19+
is responsible for the memento's safekeeping
20+
never operates on or examines the contents of a memento

Memento/Program.cs

-15
This file was deleted.

Memento/RealWorld/Memento.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace DoFactory.GangOfFour.Memento.RealWorld
2+
{
3+
/// <summary>
4+
/// The 'Memento' class
5+
/// </summary>
6+
class Memento
7+
{
8+
private string _name;
9+
private string _phone;
10+
private double _budget;
11+
12+
// Constructor
13+
public Memento(string name, string phone, double budget)
14+
{
15+
this._name = name;
16+
this._phone = phone;
17+
this._budget = budget;
18+
}
19+
20+
// Gets or sets name
21+
public string Name
22+
{
23+
get { return _name; }
24+
set { _name = value; }
25+
}
26+
27+
// Gets or set phone
28+
public string Phone
29+
{
30+
get { return _phone; }
31+
set { _phone = value; }
32+
}
33+
34+
// Gets or sets budget
35+
public double Budget
36+
{
37+
get { return _budget; }
38+
set { _budget = value; }
39+
}
40+
}
41+
}

Memento/RealWorld/Program.cs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
3+
/// <summary>
4+
/// This real-world code demonstrates the Memento pattern which temporarily saves and then restores the SalesProspect's internal state.
5+
/// </summary>
6+
namespace DoFactory.GangOfFour.Memento.RealWorld
7+
{
8+
/// <summary>
9+
/// MainApp startup class for Real-World
10+
/// Memento Design Pattern.
11+
/// </summary>
12+
class MainApp
13+
{
14+
/// <summary>
15+
/// Entry point into console application.
16+
/// </summary>
17+
static void Main()
18+
{
19+
SalesProspect s = new SalesProspect();
20+
s.Name = "Noel van Halen";
21+
s.Phone = "(412) 256-0990";
22+
s.Budget = 25000.0;
23+
24+
// Store internal state
25+
ProspectMemory m = new ProspectMemory();
26+
m.Memento = s.SaveMemento();
27+
28+
// Continue changing originator
29+
s.Name = "Leo Welch";
30+
s.Phone = "(310) 209-7111";
31+
s.Budget = 1000000.0;
32+
33+
// Restore saved state
34+
s.RestoreMemento(m.Memento);
35+
36+
// Wait for user
37+
Console.ReadKey();
38+
39+
}
40+
41+
}
42+
}
43+
44+
//Name: Noel van Halen
45+
//Phone: (412) 256-0990
46+
//Budget: 25000
47+
48+
//Saving state --
49+
50+
//Name: Leo Welch
51+
//Phone: (310) 209-7111
52+
//Budget: 1000000
53+
54+
//Restoring state --
55+
56+
//Name: Noel van Halen
57+
//Phone: (412) 256-0990
58+
//Budget: 25000

Memento/RealWorld/ProspectMemory.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace DoFactory.GangOfFour.Memento.RealWorld
2+
{
3+
/// <summary>
4+
/// The 'Caretaker' class
5+
/// </summary>
6+
class ProspectMemory
7+
{
8+
private Memento _memento;
9+
10+
// Property
11+
public Memento Memento
12+
{
13+
set { _memento = value; }
14+
get { return _memento; }
15+
}
16+
}
17+
}

Memento/RealWorld/SalesProspect.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
3+
namespace DoFactory.GangOfFour.Memento.RealWorld
4+
{
5+
/// <summary>
6+
/// The 'Originator' class
7+
/// </summary>
8+
class SalesProspect
9+
{
10+
private string _name;
11+
private string _phone;
12+
private double _budget;
13+
14+
// Gets or sets name
15+
public string Name
16+
{
17+
get { return _name; }
18+
set
19+
{
20+
_name = value;
21+
Console.WriteLine("Name: " + _name);
22+
}
23+
}
24+
25+
// Gets or sets phone
26+
public string Phone
27+
{
28+
get { return _phone; }
29+
set
30+
{
31+
_phone = value;
32+
Console.WriteLine("Phone: " + _phone);
33+
}
34+
}
35+
36+
// Gets or sets budget
37+
public double Budget
38+
{
39+
get { return _budget; }
40+
set
41+
{
42+
_budget = value;
43+
Console.WriteLine("Budget: " + _budget);
44+
}
45+
}
46+
47+
// Stores memento
48+
public Memento SaveMemento()
49+
{
50+
Console.WriteLine("\nSaving state --\n");
51+
return new Memento(_name, _phone, _budget);
52+
}
53+
54+
// Restores memento
55+
public void RestoreMemento(Memento memento)
56+
{
57+
Console.WriteLine("\nRestoring state --\n");
58+
this.Name = memento.Name;
59+
this.Phone = memento.Phone;
60+
this.Budget = memento.Budget;
61+
}
62+
}
63+
}

Memento/Structural/Caretaker.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace DoFactory.GangOfFour.Memento.Structural
2+
{
3+
/// <summary>
4+
/// The 'Caretaker' class
5+
/// </summary>
6+
class Caretaker
7+
{
8+
private Memento _memento;
9+
10+
// Gets or sets memento
11+
public Memento Memento
12+
{
13+
set { _memento = value; }
14+
get { return _memento; }
15+
}
16+
}
17+
}

Memento/Structural/Memento.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace DoFactory.GangOfFour.Memento.Structural
2+
{
3+
/// <summary>
4+
/// The 'Memento' class
5+
/// </summary>
6+
class Memento
7+
{
8+
private string _state;
9+
10+
// Constructor
11+
public Memento(string state)
12+
{
13+
this._state = state;
14+
}
15+
16+
// Gets or sets state
17+
public string State
18+
{
19+
get { return _state; }
20+
}
21+
}
22+
}

Memento/Structural/Originator.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace DoFactory.GangOfFour.Memento.Structural
4+
{
5+
/// <summary>
6+
/// The 'Originator' class
7+
/// </summary>
8+
class Originator
9+
{
10+
private string _state;
11+
12+
// Property
13+
public string State
14+
{
15+
get { return _state; }
16+
set
17+
{
18+
_state = value;
19+
Console.WriteLine("State = " + _state);
20+
}
21+
}
22+
23+
// Creates memento
24+
public Memento CreateMemento()
25+
{
26+
return (new Memento(_state));
27+
}
28+
29+
// Restores original state
30+
public void SetMemento(Memento memento)
31+
{
32+
Console.WriteLine("Restoring state...");
33+
State = memento.State;
34+
}
35+
}
36+
}

Memento/Structural/Program.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
/// <summary>
4+
/// This structural code demonstrates the Memento pattern which temporary saves and restores another object's internal state.
5+
/// </summary>
6+
namespace DoFactory.GangOfFour.Memento.Structural
7+
{
8+
/// <summary>
9+
/// MainApp startup class for Structural
10+
/// Memento Design Pattern.
11+
/// </summary>
12+
class MainApp
13+
{
14+
/// <summary>
15+
/// Entry point into console application.
16+
/// </summary>
17+
static void Main()
18+
{
19+
Originator o = new Originator();
20+
o.State = "On";
21+
22+
// Store internal state
23+
Caretaker c = new Caretaker();
24+
c.Memento = o.CreateMemento();
25+
26+
// Continue changing originator
27+
o.State = "Off";
28+
29+
// Restore saved state
30+
o.SetMemento(c.Memento);
31+
32+
// Wait for user
33+
Console.ReadKey();
34+
}
35+
}
36+
}
37+
38+
//State = On
39+
//State = Off
40+
//Restoring state:
41+
//State = On

0 commit comments

Comments
 (0)