设计模式之解释器模式(Interpreter Pattern)

代码下载

1.概念
给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。
2.类图
将字符:"选择 字段 来自 表"解释成“Select * From Table”

3.调用代码

设计模式之解释器模式(Interpreter Pattern)_语言

string strInput = "选择 字段 来自 表";
             string strOut = "";
             List<AbstractInterpreter> lst = new List<AbstractInterpreter>();
             lst.Add(new InterpreterSqlKeys());
             lst.Add(new InterpreterContent());            foreach (var item in lst)
             {
                 string temp = item.GetMatchWord(strInput);
                 strOut =  temp;
                 strInput = strOut;
             }
             this.Text = strOut;//输出 Select * From Table

代码下载