• 设计模式:解释器模式


    解释器模式(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

    测试代码:

                InterpreterDesign.Context context = new InterpreterDesign.Context();
                IList<AbstractExpression> list = new List<AbstractExpression>();
                list.Add(new TerminalExpression());
                list.Add(new NonterminalExpression());
                list.Add(new TerminalExpression());
                list.Add(new TerminalExpression());
                foreach(AbstractExpression exp in list)
                {
                    exp.Interpret(context);
                }
    View Code
  • 相关阅读:
    Hdu 3666 THE MATRIX PROBLEM(差分约束)
    POJ1201Intervals(差分约束系统)
    hdu3440 House Man 【差分约束系统】
    使用字符流读写数据
    使用带有缓冲的字节流读写数据
    使用字节流读写数据
    字符、字节与编码
    Java IO简介
    文件的简单读写
    遍历文件夹
  • 原文地址:https://www.cnblogs.com/uptothesky/p/5287279.html
Copyright © 2020-2023  润新知