-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path解释器模式.cs
66 lines (61 loc) · 1.8 KB
/
解释器模式.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
using System.Collections.Generic;
using Xunit;
namespace DesignMode
{
/// <summary>
/// 给定一个语言,定义它的文法的一种表示,并定义一个解释器,
/// 这个解释器使用该表示来解释语言中的句子.
/// 将一句话,转变成实际的命令程序执行.
/// </summary>
public class 解释器模式
{
[Fact]
public void Test()
{
Context context = new Context();
IList<AbstractExpression> list = new List<AbstractExpression>();
list.Add(new TerminamExpression());
list.Add(new NoterminamExpression());
list.Add(new TerminamExpression());
foreach (var exp in list)
{
exp.Interpret(context);
}
}
/// <summary>
/// 抽象表达式
/// </summary>
public abstract class AbstractExpression
{
public abstract void Interpret(Context context);
}
/// <summary>
/// 终端解释器
/// </summary>
public class TerminamExpression : AbstractExpression
{
public override void Interpret(Context context)
{
System.Console.WriteLine("终端解释器");
}
}
/// <summary>
/// 非终端解释器
/// </summary>
public class NoterminamExpression : AbstractExpression
{
public override void Interpret(Context context)
{
System.Console.WriteLine("非终端解释器");
}
}
/// <summary>
/// 文本内容
/// </summary>
public class Context
{
public string Input { get; set; }
public string Output { get; set; }
}
}
}