乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)
原文:乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)
作者:webabcd
介绍
给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。
示例
有一个Message实体类,某个类对它的操作有Get()方法。现在要求用具有某一规则的中文语法来执行这个操作。

MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter

{
/**//// <summary>
/// Message实体类
/// </summary>
public class MessageModel
{
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/**//// <summary>
/// Message内容
/// </summary>
public string Message
{
get
{ return _message; }
set
{ _message = value; }
}
private DateTime _publishTime;
/**//// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get
{ return _publishTime; }
set
{ _publishTime = value; }
}
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter

{
/**//// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage
{
/**//// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public static List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("SQL方式获取Message", DateTime.Now));
return l;
}
}
}
Context
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter

{
/**//// <summary>
/// Context
/// </summary>
public class Context
{
private string _input;
private string _output;

/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="input">输入内容</param>
public Context(string input)
{
this._input = input;
}

/**//// <summary>
/// 输入内容
/// </summary>
public string Input
{
get
{ return _input; }
set
{ _input = value; }
}

/**//// <summary>
/// 输出内容
/// </summary>
public string Output
{
get
{ return _output; }
set
{ _output = value; }
}
}
}
AbstractExpression
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter

{
/**//// <summary>
/// 抽象公式(AbstractExpression)
/// </summary>
public abstract class AbstractExpression
{
/**//// <summary>
/// 解释Context的方法
/// </summary>
/// <param name="context">context</param>
public void Interpret(Context context)
{
if (String.IsNullOrEmpty(context.Input))
{
return;
}
context.Output += GetCSharp(context.Input);
}

/**//// <summary>
/// 获得输入内容所对应的C#代码
/// </summary>
/// <param name="source">source</param>
/// <returns></returns>
private string GetCSharp(string source)
{
string csharp = "";
string word = "";
// 从输入内容中取得要解释的词
word = GetWord(source);
// 从字典中找到word所对应的C#代码
GetDictionary().TryGetValue(word, out csharp);
return csharp;
}

/**//// <summary>
/// 从输入内容中取得要解释的词
/// </summary>
/// <param name="source">source</param>
/// <returns></returns>
public abstract string GetWord(string source);

/**//// <summary>
/// 获取字典
/// </summary>
/// <returns></returns>
public abstract Dictionary<string, string> GetDictionary();
}
}
DatabaseExpression
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Pattern.Interpreter

{
/**//// <summary>
/// 终端公式(TerminalExpression)分析与数据库相关的
/// </summary>
public class DatabaseExpression : AbstractExpression
{
/**//// <summary>
/// 从输入内容中取得要解释的词
/// </summary>
/// <param name="source">source</param>
/// <returns></returns>
public override string GetWord(string source)
{
MatchCollection mc;
Regex r = new Regex(@"\{(.*)\}");
mc = r.Matches(source);
].Value;
}

/**//// <summary>
/// 获取与数据库相关的字典
/// </summary>
/// <returns></returns>
public override Dictionary<string, string> GetDictionary()
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("数据库", "Sql");
return d;
}
}
}
ObjectExpression
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Pattern.Interpreter

{
/**//// <summary>
/// 终端公式(TerminalExpression)分析与对象相关的
/// </summary>
public class ObjectExpression : AbstractExpression
{
/**//// <summary>
/// 从输入内容中取得要解释的词
/// </summary>
/// <param name="source">source</param>
/// <returns></returns>
public override string GetWord(string source)
{
MatchCollection mc;
Regex r = new Regex(@"\[(.*)\]");
mc = r.Matches(source);
].Value;
}

/**//// <summary>
/// 获取与对象相关的字典
/// </summary>
/// <returns></returns>
public override Dictionary<string, string> GetDictionary()
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("信息", "Message");
return d;
}
}
}
MethodExpression
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Pattern.Interpreter

{
/**//// <summary>
/// 终端公式(TerminalExpression)分析与方法相关的
/// </summary>
public class MethodExpression : AbstractExpression
{
/**//// <summary>
/// 从输入内容中取得要解释的词
/// </summary>
/// <param name="source">source</param>
/// <returns></returns>
public override string GetWord(string source)
{
MatchCollection mc;
Regex r = new Regex(@"\((.*)\)");
mc = r.Matches(source);
].Value;
}

/**//// <summary>
/// 获取与方法相关的字典
/// </summary>
/// <returns></returns>
public override Dictionary<string, string> GetDictionary()
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("获取", ".Get()");
return d;
}
}
}
client
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.CSharp;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using Pattern.Interpreter;
public partial class Interpreter : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{
string chinese = "{数据库}[信息](获取)";
Context context = new Context(chinese);
List<AbstractExpression> l = new List<AbstractExpression>();
l.Add(new DatabaseExpression());
l.Add(new ObjectExpression());
l.Add(new MethodExpression());
foreach (AbstractExpression exp in l)
{
exp.Interpret(context);
}
Assembly assembly = Assembly.Load("Pattern.Interpreter");
MethodInfo method ].Replace("()", ""));
object obj = method.Invoke(null, null);
List<MessageModel> m = (List<MessageModel>)obj;
Response.Write("中文语法:" + chinese);
Response.Write("<br />");
Response.Write("解释后的C#代码:" + context.Output);
Response.Write("<br />");
Response.Write(].PublishTime.ToString());
}
}
运行结果
中文语法:{数据库}[信息](获取)
解释后的C#代码:SqlMessage.Get()
执行结果:SQL方式获取Message 2007-5-1 8:48:07
参考
http://www.dofactory.com/Patterns/PatternInterpreter.aspx
OK
[源码下载]
乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)的更多相关文章
- C#设计模式:解释器模式(Interpreter Pattern)
一,C#设计模式:解释器模式(Interpreter Pattern) 1,解释器模式的应用场合是Interpreter模式应用中的难点,只有满足“业务规则频繁变化,且类似的模式不断重复出现,并且容易 ...
- 二十四种设计模式:解释器模式(Interpreter Pattern)
解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...
- 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)
原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)
原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 状态模式(State Pattern)
原文:乐在其中设计模式(C#) - 状态模式(State Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 状态模式(State Pattern) 作者:webabcd 介绍 允 ...
- 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)
原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
随机推荐
- JavaScript 中的事件类型2(读书笔记思维导图)
Web 浏览器中可能发生的事件有很多类型.如前所述,不同的事件类型具有不同的信息,而“ DOM3级事件”规定了以下几类事件: UI(User Interface,用户界面)事件:当用户与页面上的元素交 ...
- [C++]函数指针与指针函数
函数指针与指针函数,之前总是分不清,今天就好好把它两认认清楚. 先从概念说起,简单是理解. 函数指针:是指针.一个指向某一个函数入口地址的指针. 指针函数:是函数.一个返回值是指针的函数. 记忆: 从 ...
- [转]解决get方法传递URL参数中文乱码问题
来自:http://www.javaeye.com/topic/483158 应用一:解决tomcat下中文乱码问题(先来个简单的) 在tomcat下,我们通常这样来解决中文乱码问题: 过滤器代码: ...
- Android开发周报:反编译对抗研究、动手制作智能镜子
新闻 <Android Wear落地中国 谷歌增强安卓生态控制力> :9月8日,由摩托罗拉推出的智能手表Moto 360二代作为国内发售的第一款搭载官方Android Wear的设备,正式 ...
- 在qt中用tcp传输xml消息
在qt中用tcp传输xml消息 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:Qt5 3.1.2 说明: 在tcp上 ...
- 浅谈Swift语法
Apple 在2014年6月的WWDC公布了一款新型的开发语言,很多美国程序猿的价值观貌似和我们非常大的不同,在公布的时候我们能够听到,场下的欢呼声是接连不断的.假设换作我们,特别是像有Objecti ...
- Android 布局之LinearLayout 子控件weight权重的作用详析(转)
关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法 ...
- TextView中如何支持html标签,放置图片和动作标签
TextView文本框和输入框几乎是一个正常的带界面的可交互的Android应用的基本组成 TextView主要作用是显示文本内容,其实还可以显示图片,当然有必要的话还可以为文本内容添加动作相应用户的 ...
- HTML5 CSS3专题 纯CSS打造相冊效果
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/30993277 今天偶然发现电脑里面还有这种一个样例.感觉效果还不错,不记得啥时候 ...
- 全栈JavaScript之路(十八)HTML5 自己定义数据属性
HTML5 规范规定,用户能够为元素 自己定义非标准属性, 可是要加入 data- 前缀. 目的是为元素提供与页面渲染无关的信息.或者语义信息.这些属性名能够任意加入,仅仅要带上前缀 data- 开头 ...