乐在其中设计模式(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 ...
随机推荐
- 孙鑫HTML视频学习总结
1. HTML中元素和标签 元素是由单个或一对标签定义的包含范围.一个标签就是左右分别有一个小于号(<)和大于号(>)的字符串.开始标签是指以不以斜杠(/)开头的标签,其内是一串允许的 ...
- 内存分析工具 MAT 的使用
1 内存泄漏的排查方法 Dalvik Debug Monitor Server (DDMS) 是 ADT插件的一部分,当中有两项功能可用于内存检查 : · heap 查看堆的分配情况 · ...
- 关于SVN配置文件的一个小例子
1 背景假设 厦门央瞬公司是一家电子元器件设备供应商,其中有个ARM部门,专门负责ARM芯片的方案设计.销售,并在北京.上海各设立了一个办事处.对于工作日志,原先采用邮件方式发给经理,但是这种方式 ...
- vi 按了ctrl+s之后
再windows不管是写程序.还是用Word写文件.已经习惯了按ctrl+s 保存代码. 在用vi的时候.常常无意中按了ctrl+s,结果就是如同终端死掉了一样. 这是由于ctrl+s 终止屏幕输出( ...
- java环境变量设置--编写一年java,竟不会配变量了
java环境变量设置 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:C:\Program Files\Ja ...
- Jetty开发指导:Jetty Websocket API
Jetty WebSocket API使用 Jetty提供了功能更强的WebSocket API,使用一个公共的核心API供WebSockets的服务端和client使用. 他是一个基于WebSock ...
- 使用zTree和json构建简单树节点
我们经常碰到须要构建树结构展示的情况,我推荐使用zTree和JSON. 比如: <? php /** * * 使用zTree和json构建树节点 * */ $arr = array( 0=> ...
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...
- 搭建ganglia集群而且监视hadoop CDH4.6
前言 近期在研究云监控的相关工具,感觉ganglia颇有亮点,能从一个集群总体的角度来展现数据. 但是安装过程稍过复杂,相关依赖稍多,故写此文章与大家分享下. 本文不解说相关原理,若想了解请參考其它资 ...
- Windows Phone开发(29):隔离存储C
原文:Windows Phone开发(29):隔离存储C 本文是隔离存储的第三节,大家先喝杯咖啡放松,今天的内容也是非常简单,我们就聊一件东东--用户设置. 当然了,可能翻译为应用程序设置合适一些,不 ...