forked from EdisonTalk/DesignPattern.Samples.CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
101 lines (85 loc) · 2.91 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Manulife.ChengDu.DesignPattern.Memento
{
public class Program
{
public static void Main(string[] args)
{
//SingleRedoDemo();
MultiRedoDemo();
Console.ReadKey();
}
#region v1.0 实现单次撤销
public static void SingleRedoDemo()
{
MementoCaretaker mc = new MementoCaretaker();
Chessman chess = new Chessman("车", 1, 1);
Display(chess);
// 保存状态
mc.Memento = chess.Save();
chess.Y = 4;
Display(chess);
// 保存状态
mc.Memento = chess.Save();
Display(chess);
chess.X = 5;
Display(chess);
Console.WriteLine("---------- Sorry,俺悔棋了 ---------");
// 恢复状态
chess.Restore(mc.Memento);
Display(chess);
}
public static void Display(Chessman chess)
{
Console.WriteLine("棋子 {0} 当前位置为:第 {1} 行 第 {2} 列", chess.Label, chess.X, chess.Y);
}
#endregion
#region v2.0 实现多次撤销
private static int index = -1;
private static NewMementoCaretaker mementoCaretaker = new NewMementoCaretaker();
public static void MultiRedoDemo()
{
Chessman chess = new Chessman("车", 1, 1);
Play(chess);
chess.Y = 4;
Play(chess);
chess.X = 5;
Play(chess);
Undo(chess, index);
Undo(chess, index);
Redo(chess, index);
Redo(chess, index);
}
// 下棋
public static void Play(Chessman chess)
{
// 保存备忘录
mementoCaretaker.SetMemento(chess.Save());
index++;
Console.WriteLine("棋子 {0} 当前位置为 第 {1} 行 第 {2} 列", chess.Label, chess.X, chess.Y);
}
// 悔棋
public static void Undo(Chessman chess, int i)
{
Console.WriteLine("---------- Sorry,俺悔棋了 ---------");
index--;
// 撤销到上一个备忘录
chess.Restore(mementoCaretaker.GetMemento(i - 1));
Console.WriteLine("棋子 {0} 当前位置为 第 {1} 行 第 {2} 列", chess.Label, chess.X, chess.Y);
}
// 撤销悔棋
public static void Redo(Chessman chess, int i)
{
Console.WriteLine("---------- Sorry,撤销悔棋 ---------");
index++;
// 恢复到下一个备忘录
chess.Restore(mementoCaretaker.GetMemento(i + 1));
Console.WriteLine("棋子 {0} 当前位置为 第 {1} 行 第 {2} 列", chess.Label, chess.X, chess.Y);
}
#endregion
}
}