乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)-阿里云开发者社区

开发者社区> 数据库> 正文
登录阅读全文

乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)

简介:
[索引页]
[源码下载]


乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)


作者:webabcd


介绍
给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。


示例
有一个Message实体类,某个类对它的操作有Get()方法。现在要求用具有某一规则的中文语法来执行这个操作。
 
MessageModel
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Interpreter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// Message实体类 
InBlock.gif        /// </summary> 
InBlock.gif        public class MessageModel 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="msg">Message内容</param> 
InBlock.gif                /// <param name="pt">Message发布时间</param> 
InBlock.gif                public MessageModel(string msg, DateTime pt) 
InBlock.gif                { 
InBlock.gif                        this._message = msg; 
InBlock.gif                        this._publishTime = pt; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private string _message; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message内容 
InBlock.gif                /// </summary> 
InBlock.gif                public string Message 
InBlock.gif                { 
InBlock.gif                        get { return _message; } 
InBlock.gif                        set { _message = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private DateTime _publishTime; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message发布时间 
InBlock.gif                /// </summary> 
InBlock.gif                public DateTime PublishTime 
InBlock.gif                { 
InBlock.gif                        get { return _publishTime; } 
InBlock.gif                        set { _publishTime = value; } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
SqlMessage
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Interpreter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// Sql方式操作Message 
InBlock.gif        /// </summary> 
InBlock.gif        public class SqlMessage 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取Message 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public static List<MessageModel> Get() 
InBlock.gif                { 
InBlock.gif                        List<MessageModel> l = new List<MessageModel>(); 
InBlock.gif                        l.Add(new MessageModel("SQL方式获取Message", DateTime.Now)); 
InBlock.gif 
InBlock.gif                        return l; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Context
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Interpreter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// Context 
InBlock.gif        /// </summary> 
InBlock.gif        public class Context 
InBlock.gif        { 
InBlock.gif                private string _input; 
InBlock.gif                private string _output; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="input">输入内容</param> 
InBlock.gif                public Context(string input) 
InBlock.gif                { 
InBlock.gif                        this._input = input; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 输入内容 
InBlock.gif                /// </summary> 
InBlock.gif                public string Input 
InBlock.gif                { 
InBlock.gif                        get { return _input; } 
InBlock.gif                        set { _input = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 输出内容 
InBlock.gif                /// </summary> 
InBlock.gif                public string Output 
InBlock.gif                { 
InBlock.gif                        get { return _output; } 
InBlock.gif                        set { _output = value; } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
AbstractExpression
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Interpreter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 抽象公式(AbstractExpression) 
InBlock.gif        /// </summary> 
InBlock.gif        public abstract class AbstractExpression 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 解释Context的方法 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="context">context</param> 
InBlock.gif                public void Interpret(Context context) 
InBlock.gif                { 
InBlock.gif                        if (String.IsNullOrEmpty(context.Input)) 
InBlock.gif                        { 
InBlock.gif                                return
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        context.Output += GetCSharp(context.Input); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获得输入内容所对应的C#代码 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="source">source</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                private string GetCSharp(string source) 
InBlock.gif                { 
InBlock.gif                        string csharp = ""; 
InBlock.gif                        string word = ""; 
InBlock.gif 
InBlock.gif                        // 从输入内容中取得要解释的词 
InBlock.gif                        word = GetWord(source); 
InBlock.gif 
InBlock.gif                        // 从字典中找到word所对应的C#代码 
InBlock.gif                        GetDictionary().TryGetValue(word, out csharp); 
InBlock.gif 
InBlock.gif                        return csharp; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 从输入内容中取得要解释的词 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="source">source</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public abstract string GetWord(string source); 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取字典 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public abstract Dictionary<stringstring> GetDictionary(); 
InBlock.gif        } 
InBlock.gif}
 
DatabaseExpression
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Text.RegularExpressions; 
InBlock.gif 
InBlock.gifnamespace Pattern.Interpreter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 终端公式(TerminalExpression)分析与数据库相关的 
InBlock.gif        /// </summary> 
InBlock.gif        public class DatabaseExpression : AbstractExpression 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 从输入内容中取得要解释的词 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="source">source</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public override string GetWord(string source) 
InBlock.gif                { 
InBlock.gif                        MatchCollection mc; 
InBlock.gif                        Regex r = new Regex(@"\{(.*)\}"); 
InBlock.gif                        mc = r.Matches(source); 
InBlock.gif 
InBlock.gif                        return mc[0].Groups[1].Value; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取与数据库相关的字典 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public override Dictionary<stringstring> GetDictionary() 
InBlock.gif                { 
InBlock.gif                        Dictionary<stringstring> d = new Dictionary<stringstring>(); 
InBlock.gif 
InBlock.gif                        d.Add("数据库""Sql"); 
InBlock.gif 
InBlock.gif                        return d; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
ObjectExpression
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Text.RegularExpressions; 
InBlock.gif 
InBlock.gifnamespace Pattern.Interpreter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 终端公式(TerminalExpression)分析与对象相关的 
InBlock.gif        /// </summary> 
InBlock.gif        public class ObjectExpression : AbstractExpression 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 从输入内容中取得要解释的词 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="source">source</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public override string GetWord(string source) 
InBlock.gif                { 
InBlock.gif                        MatchCollection mc; 
InBlock.gif                        Regex r = new Regex(@"\[(.*)\]"); 
InBlock.gif                        mc = r.Matches(source); 
InBlock.gif 
InBlock.gif                        return mc[0].Groups[1].Value; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取与对象相关的字典 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public override Dictionary<stringstring> GetDictionary() 
InBlock.gif                { 
InBlock.gif                        Dictionary<stringstring> d = new Dictionary<stringstring>(); 
InBlock.gif 
InBlock.gif                        d.Add("信息""Message"); 
InBlock.gif 
InBlock.gif                        return d; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
MethodExpression
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Text.RegularExpressions; 
InBlock.gif 
InBlock.gifnamespace Pattern.Interpreter 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 终端公式(TerminalExpression)分析与方法相关的 
InBlock.gif        /// </summary> 
InBlock.gif        public class MethodExpression : AbstractExpression 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 从输入内容中取得要解释的词 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="source">source</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public override string GetWord(string source) 
InBlock.gif                { 
InBlock.gif                        MatchCollection mc; 
InBlock.gif                        Regex r = new Regex(@"\((.*)\)"); 
InBlock.gif                        mc = r.Matches(source); 
InBlock.gif 
InBlock.gif                        return mc[0].Groups[1].Value; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取与方法相关的字典 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public override Dictionary<stringstring> GetDictionary() 
InBlock.gif                { 
InBlock.gif                        Dictionary<stringstring> d = new Dictionary<stringstring>(); 
InBlock.gif 
InBlock.gif                        d.Add("获取"".Get()"); 
InBlock.gif 
InBlock.gif                        return d; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
client
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifusing Microsoft.CSharp; 
InBlock.gifusing System.Reflection; 
InBlock.gifusing System.Text; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gif 
InBlock.gifusing Pattern.Interpreter; 
InBlock.gif 
InBlock.gifpublic partial class Interpreter : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                string chinese = "{数据库}[信息](获取)"
InBlock.gif                Context context = new Context(chinese); 
InBlock.gif 
InBlock.gif                List<AbstractExpression> l = new List<AbstractExpression>(); 
InBlock.gif                l.Add(new DatabaseExpression()); 
InBlock.gif                l.Add(new ObjectExpression()); 
InBlock.gif                l.Add(new MethodExpression()); 
InBlock.gif 
InBlock.gif                foreach (AbstractExpression exp in l) 
InBlock.gif                { 
InBlock.gif                        exp.Interpret(context); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                Assembly assembly = Assembly.Load("Pattern.Interpreter"); 
InBlock.gif                MethodInfo method = assembly.GetType("Pattern.Interpreter." + context.Output.Split('.')[0]).GetMethod(context.Output.Split('.')[1].Replace("()", "")); 
InBlock.gif                object obj = method.Invoke(nullnull); 
InBlock.gif 
InBlock.gif                List<MessageModel> m = (List<MessageModel>)obj; 
InBlock.gif 
InBlock.gif                Response.Write("中文语法:" + chinese); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write("解释后的C#代码:" + context.Output); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write("执行结果:" + m[0].Message + " " + m[0].PublishTime.ToString()); 
InBlock.gif        } 
InBlock.gif}
 
 
运行结果
中文语法:{数据库}[信息](获取)
解释后的C#代码:SqlMessage.Get()
执行结果:SQL方式获取Message 2007-5-1 8:48:07 


参考
http://www.dofactory.com/Patterns/PatternInterpreter.aspx


OK
[源码下载]
 


     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344555,如需转载请自行联系原作者


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

其他文章
最新文章
相关文章