using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DesignPattern
{
//解释器模式
//给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子
internal class InterpretPattern
{
}
//文法规则 和 抽象语法树
//每个文法规则的语言实例都可以表示为一棵抽象语法树
//抽象语法树描述了如何构成一个复杂的句子,通过分析抽象语法树的分析,可以识别出语言中的终结符类和非终结符类
//抽象节点类
abstract class AbstractNode
{
public abstract string Interpret();
}
//And节点类 非终结符表达式角色
class AndNode : AbstractNode
{
private AbstractNode left;//and 的左表达式
private AbstractNode right; //and 的右表达式
public AndNode(AbstractNode left, AbstractNode right)
{
this.left = left;
this.right = right;
}
//AND表达式解释操作
public override string Interpret()
{
return left.Interpret() + "再" + right.Interpret();
}
}
//简单句子节点类 充当非终结符表达式角色
class SentenceNode : AbstractNode
{
private AbstractNode direction;
private AbstractNode action;
private AbstractNode distance;
public SentenceNode(AbstractNode direction, AbstractNode action, AbstractNode distance)
{
this.direction = direction;
this.action = action;
this.distance = distance;
}
public override string Interpret()
{
return direction.Interpret() + action.Interpret() + distance.Interpret();
}
}
//方向节点类 充当终结符表达式角色。
class DirectionNode : AbstractNode
{
private string direction;
public DirectionNode(string direction)
{
this.direction = direction;
}
//方向表达式的解释操作
public override string Interpret()
{
if (direction.Equals("up"))
{
return "向上";
}
else if (direction.Equals("down"))
{
return "向下";
}
else
if (direction.Equals("left"))
{
return "向左";
}
else if (direction.Equals("right"))
{
return "向右";
}
else
{
return "无效指令";
}
}
}
//动作节点类 充当终结符表达式角色
class ActionNode : AbstractNode
{
private string action;
public ActionNode(string action)
{
this.action = action;
}
//移动方式表达式的解释操作
public override string Interpret()
{
if (action.Equals("move"))
{
return "移动";
}
else if (action.Equals("run"))
{
return "快速移动";
}
else
{
return "无效指令";
}
}
}
//距离节点类 充当终结符表达式角色
class DistanceNode : AbstractNode
{
private string distance;
public DistanceNode(string distance)
{
this.distance = distance;
}
//距离表达式的解释操作
public override string Interpret()
{
return this.distance;
}
}
//指令处理类 工具类 提供相应的方法对输入的指令进行处理。
class InstructionHandler
{
private AbstractNode node;
public void Handle(string instruction)
{
AbstractNode left = null, right = null;
AbstractNode direction = null, action = null, distance = null;
System.Collections.Stack stack = new System.Collections.Stack(); //声明一个栈对象,用于存储抽象语法树
string[] words = instruction.Split(' ');//以空格分隔指令字符串
for (int i = 0; i < words.Count(); i++)
{
//本实例采用栈的方式类处理指令,如果遇到AND。则将其后的三个单词作为3格终结符表达式连城一个简单句子SentanceNode作为AND的右表达式
//而将从栈顶弹出的表达式作为AND的左表达式,最后将新的AND表达式压入栈中
if (words[i].Equals("and"))
{
left = (AbstractNode)stack.Pop();//弹出栈顶表达式为左表达式
string word1 = words[++i];
direction = new DirectionNode(word1);
string word2 = words[++i];
action = new ActionNode(word2);
string word3 = words[++i];
distance = new DistanceNode(word3);
right = new SentenceNode(direction, action, distance);//右表达式
stack.Push(new AndNode(left, right));//将新表达式压入栈中
}
else {
//如果是从头开始进行解释,则将前三个单词组成一个简单句子SentanceNode,并将该句压入栈中
string word1 = words[i];
direction = new DirectionNode(word1);
string word2 = words[++i];
action = new ActionNode(word2);
string word3 = words[++i];
distance = new DistanceNode(word3);
left = new SentenceNode(direction, action, distance);//左表达式
stack.Push(left);
}
}
this.node = (AbstractNode)stack.Pop();//将全部表达式从栈中弹出
}
public string Output() {
string result=node.Interpret();
return result;
}
}
class Progarm1 {
public void main() {
//客户端测试
string instruction = "down run 10 and left move 20";
InstructionHandler handler = new InstructionHandler();
handler.Handle(instruction);
string outString=handler.Output();
Console.WriteLine(outString);
Console.ReadKey();
//输出
//向下快速移动10 再向左移动20
//如果输入的指令改为“up move 5 and down run 10 and left move 5”
//输出
//向上移动5再向下快速移动10再向左移动5
}
}
}
《C#设计模式》==>解释器模式
最新推荐文章于 2023-06-14 10:09:35 发布