解释器模式(Interpreter):给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。

设计模式:解释器模式

namespace InterpreterDesign
{
    public abstract class AbstractExpression
    {
        public abstract void Interpret(Context context);
    }
    public class TerminalExpression : AbstractExpression
    {
        public override void Interpret(Context context)
        {
            Console.WriteLine("终端解释器");
        }
    }
    public class NonterminalExpression:AbstractExpression
    {
        public override void Interpret(Context context)
        {
            Console.WriteLine("非终端解释器");
        }
    }
    public class Context
    {
        private string input;
        public string Input
        {
            get { return input; }
            set { input = value; }
        }
        private string output;
        public string Output
        {
            get { return output; }
            set { output = value; }
        }
    }
}
View Code

相关文章:

  • 2021-10-29
  • 2021-04-11
  • 2022-01-15
猜你喜欢
  • 2021-12-17
  • 2021-09-08
  • 2021-07-18
  • 2021-08-31
  • 2021-09-27
相关资源
相似解决方案