Skip to content

Commit 4bf367a

Browse files
committed
Update memento pattern examples
1 parent 1a80924 commit 4bf367a

File tree

11 files changed

+33
-67
lines changed

11 files changed

+33
-67
lines changed

src/BehavioralPatterns/Memento/MementoLibrary/ConceptualExample/Caretaker.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,7 @@ public void Undo()
3232

3333
var memento = _mementos.Pop();
3434
Console.WriteLine($"Caretaker: Restoring state to: {memento.GetName()}");
35-
36-
try
37-
{
38-
_originator.Restore(memento);
39-
}
40-
catch (Exception)
41-
{
42-
Undo();
43-
}
35+
_originator.Restore(memento);
4436
}
4537

4638
public void ShowHistory()
@@ -52,4 +44,4 @@ public void ShowHistory()
5244
Console.WriteLine(memento.GetName());
5345
}
5446
}
55-
}
47+
}

src/BehavioralPatterns/Memento/MementoLibrary/ConceptualExample/ConceptualExecutor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public static void Execute()
88
{
99
ConsoleExtension.WriteSeparator("Conceptual example");
1010

11-
Originator originator = new Originator("Initial state.");
12-
Caretaker caretaker = new Caretaker(originator);
11+
var originator = new Originator("Initial state.");
12+
var caretaker = new Caretaker(originator);
1313
caretaker.Backup();
1414

1515
originator.DoSomething();
@@ -25,7 +25,7 @@ public static void Execute()
2525
Console.WriteLine("\nClient: Now, let's rollback!\n");
2626
caretaker.Undo();
2727

28-
Console.WriteLine("\nClient: Once more!\n");
28+
Console.WriteLine("\n\nClient: Rollback once more!\n");
2929
caretaker.Undo();
3030
}
31-
}
31+
}

src/BehavioralPatterns/Memento/MementoLibrary/ConceptualExample/ConcreteMemento.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,17 @@ public ConcreteMemento(string state)
2121
/// The Originator uses this method when restoring its state.
2222
/// </summary>
2323
/// <returns>State.</returns>
24-
public string GetState()
25-
{
26-
return _state;
27-
}
24+
public string GetState() => _state;
2825

2926
/// <summary>
3027
/// Used by the Caretaker to display metadata.
3128
/// </summary>
3229
/// <returns>Name.</returns>
33-
public string GetName()
34-
{
35-
return $"{_creationDate.ToString("MM/dd/yyyy hh:mm:ss.fff tt")} / ({_state.Substring(0, 9)})...";
36-
}
30+
public string GetName() => $"{_creationDate:MM/dd/yyyy hh:mm:ss.fff tt} / ({_state[..9]})...";
3731

3832
/// <summary>
3933
/// Used by the Caretaker to display metadata.
4034
/// </summary>
4135
/// <returns>Creation date.</returns>
42-
public DateTime GetCreationDate()
43-
{
44-
return _creationDate;
45-
}
46-
}
36+
public DateTime GetCreationDate() => _creationDate;
37+
}

src/BehavioralPatterns/Memento/MementoLibrary/ConceptualExample/IMemento.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
public interface IMemento
99
{
1010
DateTime GetCreationDate();
11-
1211
string GetName();
13-
}
12+
}

src/BehavioralPatterns/Memento/MementoLibrary/ConceptualExample/Originator.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ public class Originator
1111
/// For the sake of simplicity, the originator's state is stored inside a single variable.
1212
/// </summary>
1313
private string _state;
14-
15-
private static readonly Random random = new Random();
14+
private static readonly Random Random = new();
1615

1716
public Originator(string state)
1817
{
@@ -28,20 +27,15 @@ public Originator(string state)
2827
public void DoSomething()
2928
{
3029
Console.WriteLine("\nOriginator: I'm doing something important.");
31-
3230
_state = GenerateRandomString(30);
33-
3431
Console.WriteLine($"Originator: and my state has changed to: {_state}");
3532
}
3633

3734
/// <summary>
3835
/// Saves the current state inside a memento.
3936
/// </summary>
4037
/// <returns>Memento.</returns>
41-
public IMemento Save()
42-
{
43-
return new ConcreteMemento(_state);
44-
}
38+
public IMemento Save() => new ConcreteMemento(_state);
4539

4640
/// <summary>
4741
/// Restores the Originator's state from a memento object.
@@ -51,21 +45,22 @@ public void Restore(IMemento memento)
5145
{
5246
if (memento is not ConcreteMemento concreteMemento)
5347
{
54-
throw new Exception($"Unknown memento: {memento}");
48+
throw new ArgumentException($"Unknown memento: {memento}");
5549
}
5650

5751
_state = concreteMemento.GetState();
5852
Console.Write($"Originator: My state has changed to: {_state}");
5953
}
6054

61-
private string GenerateRandomString(int length = 10)
55+
private static string GenerateRandomString(int length = 10)
6256
{
63-
string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
57+
const string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
6458

65-
var randomCharacters = Enumerable.Repeat(characters, length)
66-
.Select(s => s[random.Next(s.Length)])
59+
var randomCharacters = Enumerable
60+
.Repeat(characters, length)
61+
.Select(s => s[Random.Next(s.Length)])
6762
.ToArray();
6863

6964
return new string(randomCharacters);
7065
}
71-
}
66+
}

src/BehavioralPatterns/Memento/MementoLibrary/Executor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ public override void Execute()
1313
ConceptualExecutor.Execute();
1414
FoodSupplierExecutor.Execute();
1515
}
16-
}
16+
}

src/BehavioralPatterns/Memento/MementoLibrary/FoodSupplierExample/FoodSupplier.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
public class FoodSupplier
44
{
5-
private string _name;
6-
private string _phone;
7-
private string _address;
5+
private string _name = string.Empty;
6+
private string _phone = string.Empty;
7+
private string _address = string.Empty;
88

99
public string Name
1010
{
11-
get
12-
{
13-
return _name;
14-
}
11+
get => _name;
1512
set
1613
{
1714
_name = value;
@@ -21,10 +18,7 @@ public string Name
2118

2219
public string Phone
2320
{
24-
get
25-
{
26-
return _phone;
27-
}
21+
get => _phone;
2822
set
2923
{
3024
_phone = value;
@@ -34,10 +28,7 @@ public string Phone
3428

3529
public string Address
3630
{
37-
get
38-
{
39-
return _address;
40-
}
31+
get => _address;
4132
set
4233
{
4334
_address = value;
@@ -64,7 +55,7 @@ public IMemento Restore(IMemento memento)
6455

6556
if (memento is not FoodSupplierMemento foodSupplierMemento)
6657
{
67-
throw new Exception($"Unknown memento: {memento}");
58+
throw new ArgumentException($"Unknown memento: {memento}");
6859
}
6960

7061
var redoMemento = new FoodSupplierMemento(_name, _phone, _address);
@@ -75,4 +66,4 @@ public IMemento Restore(IMemento memento)
7566

7667
return redoMemento;
7768
}
78-
}
69+
}

src/BehavioralPatterns/Memento/MementoLibrary/FoodSupplierExample/FoodSupplierExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ public static void Execute()
2323
registry.Undo();
2424
registry.Redo();
2525
}
26-
}
26+
}

src/BehavioralPatterns/Memento/MementoLibrary/FoodSupplierExample/FoodSupplierMemento.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public FoodSupplierMemento(string name, string phoneNumber, string address)
1010
}
1111

1212
public string Name { get; set; }
13-
1413
public string PhoneNumber { get; set; }
15-
1614
public string Address { get; set; }
17-
}
15+
}

src/BehavioralPatterns/Memento/MementoLibrary/FoodSupplierExample/IMemento.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
public interface IMemento
44
{
5-
}
5+
}

0 commit comments

Comments
 (0)