-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
41 lines (26 loc) · 839 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Originator<int> originator = new Originator<int>();
originator.SetState(1);
CareTaker<int> careTaker = new CareTaker<int>();
careTaker.SetMemento(originator.Redo());
Console.WriteLine(originator.GetState());
originator.SetState(2);
Console.WriteLine(originator.GetState());
originator.Undo(careTaker.GetMemento());
Console.WriteLine(originator.GetState());
record Memento<T>(T state)
{
}
class Originator<T>
{
private T _state { get; set; }
public T GetState() => _state;
public void SetState(T state) => _state = state;
public Memento<T> Redo() => new(_state);
public void Undo(Memento<T> memento) => _state = memento.state;
}
class CareTaker<T>
{
private Memento<T> _memento;
public Memento<T> GetMemento() => _memento;
public void SetMemento(Memento<T> memento) => _memento = memento;
}