forked from WeihanLi/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicalExpression.cs
More file actions
111 lines (92 loc) · 2.69 KB
/
MusicalExpression.cs
File metadata and controls
111 lines (92 loc) · 2.69 KB
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
102
103
104
105
106
107
108
109
110
111
using System;
namespace InterpreterPattern
{
internal abstract class MusicalExpression
{
public virtual void Interpret(PlayContext context)
{
if (string.IsNullOrEmpty(context.PlayText))
{
return;
}
var playKey = context.PlayText.Substring(0, 1);
context.PlayText = context.PlayText.Substring(2);
var playValue = context.PlayText.IndexOf(" ", StringComparison.Ordinal) > 0 ? Convert.ToDouble(context.PlayText.Substring(0, context.PlayText.IndexOf(" ", StringComparison.Ordinal))) : 0;
context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ", StringComparison.Ordinal) + 1);
Execute(playKey, playValue);
}
public abstract void Execute(string key, double value);
}
internal class MusicalNote : MusicalExpression
{
public override void Execute(string key, double value)
{
var note = string.Empty;
switch (key)
{
case "C":
note = "1";
break;
case "D":
note = "2";
break;
case "E":
note = "3";
break;
case "F":
note = "4";
break;
case "G":
note = "5";
break;
case "A":
note = "6";
break;
case "B":
note = "7";
break;
}
Console.Write(note + " ");
}
}
internal class MusicalScale : MusicalExpression
{
public override void Execute(string key, double value)
{
var scale = string.Empty;
switch (value)
{
case 1:
scale = "低音";
break;
case 2:
scale = "中音";
break;
case 3:
scale = "高音";
break;
}
Console.Write(scale + " ");
}
}
internal class MusicalSpeed : MusicalExpression
{
public override void Execute(string key, double value)
{
string speed;
if (value < 500)
{
speed = "快速";
}
else if (value >= 1000)
{
speed = "快速";
}
else
{
speed = "中速";
}
Console.Write(speed + " ");
}
}
}