解释器模式(Interpreter Pattern)
解释器模式(Interpreter Pattern)是一种行为型模式,定义了一个解释器,来解释给定语言和文法的句子。其实质是把语言中的每个符号定义成一个(对象)类,从而把每个程序转换成一个具体的对象树。解释器模式的作用很强大,它使得改变和扩展文法变得容易,很多编译器,包括文本编辑器、网页浏览器及VRML都应用解释器模式。因文句会分析成树结构,解释器需要递归访问它,则效率会受影响。程序开发时会有所体会,编译整个工程源码耗费时间都比较长。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
//一般每个接口或类都写在单独的.cs文件中
//本示例为了执行查看方便才写在一起
/// <summary>
/// 解释器抽象类
/// </summary>
abstract class AbstractionExpression
{
public abstract string Interpret();
}
/// <summary>
/// 用于解释具体的某个指令
/// </summary>
class TerminalExpression : AbstractionExpression
{
AbstractionExpression _Dir, _RunMode, _Dis;
public TerminalExpression(AbstractionExpression dir, AbstractionExpression runMode, AbstractionExpression dis)
{
this._Dir = dir;
this._RunMode = runMode;
this._Dis = dis;
}
public override string Interpret()
{
return string.Concat(_Dir.Interpret(), _RunMode.Interpret(), _Dis.Interpret());
}
}
/// <summary>
/// 用于将终结符表达式连接起来
/// </summary>
class NonTerminalExpression : AbstractionExpression
{
AbstractionExpression _Left, _Right;
public NonTerminalExpression(AbstractionExpression left, AbstractionExpression right)
{
this._Left = left;
this._Right = right;
}
public override string Interpret()
{
return $"{this._Left.Interpret()}然后{ this._Right.Interpret()} ";
}
}
/// <summary>
/// 终结符表达式中需要用到的表达式
/// </summary>
class DirExpression : AbstractionExpression
{
string _Dir;
public DirExpression(string dir)
{
this._Dir = dir;
}
public override string Interpret()
{
if (this._Dir == "up")
{
return "向上";
}
else if (this._Dir == "down")
{
return "向下";
}
else if (this._Dir == "left")
{
return "向左";
}
else if (this._Dir == "right")
{
return "向右";
}
else
{
return "方向错误";
}
}
}
class RunModeExpression : AbstractionExpression
{
public string _RunMode;
public RunModeExpression(string runMode)
{
this._RunMode = runMode;
}
public override string Interpret()
{
if (this._RunMode == "move")
{
return "移动";
}
else if (this._RunMode == "run")
{
return "快速移动";
}
else
{
return "运动模式错误";
}
}
}
class DisExpression : AbstractionExpression
{
string _Dis;
public DisExpression(string dis)
{
this._Dis = dis;
}
public override string Interpret()
{
return $"距离为{this._Dis }";
}
}
/// <summary>
/// 上下文环境类
/// </summary>
public class HandleExpression
{
Stack<AbstractionExpression> stack = new Stack<AbstractionExpression>();//使用栈类实现递归
AbstractionExpression _dir, _runMode, _dis;
AbstractionExpression allExpression;
public void Operate(string expression)
{
string[] str = expression.Split(' ');
for (int i = 0; i < str.Length; i++)
{
if (str[i] == "and")
{
AbstractionExpression left = stack.Pop();
_dir = new DirExpression(str[++i]);
_runMode = new RunModeExpression(str[++i]);
_dis = new DisExpression(str[++i]);
AbstractionExpression right = new TerminalExpression(_dir, _runMode, _dis);
AbstractionExpression endExpression = new NonTerminalExpression(left, right);
stack.Push(endExpression);
}
else
{
_dir = new DirExpression(str[i]);
_runMode = new RunModeExpression(str[++i]);
_dis = new DisExpression(str[++i]);
AbstractionExpression endExpression = new TerminalExpression(_dir, _runMode, _dis);
stack.Push(endExpression);
}
}
allExpression = stack.Pop();//弹出所有表达式
}
public string InterpretAll()
{
return allExpression.Interpret();
}
}
class Program
{
static void Main(string[] args)
{
HandleExpression handleExpression = new HandleExpression();
string expression = "up move 230 and down run 640";
handleExpression.Operate(expression);
string result = handleExpression.InterpretAll();
Console.WriteLine(result);
Console.ReadKey();
}
}
}