天天看点

2022行为型模式C#解释器模式(Interpreter Pattern),不进来?

作者:王東東Rex

给定一种语言,解释器设计模式定义其语法的表示形式以及使用该表示形式来解释语言中的句子的解释器。

UML类图

2022行为型模式C#解释器模式(Interpreter Pattern),不进来?

C# 中的结构代码

#

此结构代码演示了解释器模式,该模式使用定义的语法,提供处理已分析语句的解释器。

  • using System;
  • using System.Collections.Generic;
  • namespace Interpreter.Structural
  • {
  • /// <summary>
  • /// Interpreter Design Pattern
  • /// </summary>
  • public class Program
  • public static void Main(string[] args)
  • Context context = new Context();
  • // Usually a tree
  • List<AbstractExpression> list = new List<AbstractExpression>();
  • // Populate 'abstract syntax tree'
  • list.Add(new TerminalExpression());
  • list.Add(new NonterminalExpression());
  • // Interpret
  • foreach (AbstractExpression exp in list)
  • exp.Interpret(context);
  • }
  • // Wait for user
  • Console.ReadKey();
  • /// The 'Context' class
  • public class Context
  • /// The 'AbstractExpression' abstract class
  • public abstract class AbstractExpression
  • public abstract void Interpret(Context context);
  • /// The 'TerminalExpression' class
  • public class TerminalExpression : AbstractExpression
  • public override void Interpret(Context context)
  • Console.WriteLine("Called Terminal.Interpret()");
  • /// The 'NonterminalExpression' class
  • public class NonterminalExpression : AbstractExpression
  • Console.WriteLine("Called Nonterminal.Interpret()");
2022行为型模式C#解释器模式(Interpreter Pattern),不进来?

C# 中的实际代码

此实际代码演示了用于将罗马数字转换为十进制的解释器模式。

  • namespace Interpreter.RealWorld
  • string roman = "MCMXXVIII";
  • Context context = new Context(roman);
  • // Build the 'parse tree'
  • List<Expression> tree = new List<Expression>();
  • tree.Add(new ThousandExpression());
  • tree.Add(new HundredExpression());
  • tree.Add(new TenExpression());
  • tree.Add(new OneExpression());
  • foreach (Expression exp in tree)
  • Console.WriteLine("{0} = {1}",
  • roman, context.Output);
  • string input;
  • int output;
  • // Constructor
  • public Context(string input)
  • this.input = input;
  • public string Input
  • get { return input; }
  • set { input = value; }
  • public int Output
  • get { return output; }
  • set { output = value; }
  • /// The 'AbstractExpression' class
  • public abstract class Expression
  • public void Interpret(Context context)
  • if (context.Input.Length == 0)
  • return;
  • if (context.Input.StartsWith(Nine()))
  • context.Output += (9 * Multiplier());
  • context.Input = context.Input.Substring(2);
  • else if (context.Input.StartsWith(Four()))
  • context.Output += (4 * Multiplier());
  • else if (context.Input.StartsWith(Five()))
  • context.Output += (5 * Multiplier());
  • context.Input = context.Input.Substring(1);
  • while (context.Input.StartsWith(One()))
  • context.Output += (1 * Multiplier());
  • public abstract string One();
  • public abstract string Four();
  • public abstract string Five();
  • public abstract string Nine();
  • public abstract int Multiplier();
  • /// A 'TerminalExpression' class
  • /// <remarks>
  • /// Thousand checks for the Roman Numeral M
  • /// </remarks>
  • public class ThousandExpression : Expression
  • public override string One() { return "M"; }
  • public override string Four() { return " "; }
  • public override string Five() { return " "; }
  • public override string Nine() { return " "; }
  • public override int Multiplier() { return 1000; }
  • /// Hundred checks C, CD, D or CM
  • public class HundredExpression : Expression
  • public override string One() { return "C"; }
  • public override string Four() { return "CD"; }
  • public override string Five() { return "D"; }
  • public override string Nine() { return "CM"; }
  • public override int Multiplier() { return 100; }
  • /// Ten checks for X, XL, L and XC
  • public class TenExpression : Expression
  • public override string One() { return "X"; }
  • public override string Four() { return "XL"; }
  • public override string Five() { return "L"; }
  • public override string Nine() { return "XC"; }
  • public override int Multiplier() { return 10; }
  • /// One checks for I, II, III, IV, V, VI, VI, VII, VIII, IX
  • public class OneExpression : Expression
  • public override string One() { return "I"; }
  • public override string Four() { return "IV"; }
  • public override string Five() { return "V"; }
  • public override string Nine() { return "IX"; }
  • public override int Multiplier() { return 1; }
2022行为型模式C#解释器模式(Interpreter Pattern),不进来?

继续阅读