解释器解释语法一
[源码下载]
设计模式 - Interpreter Pattern(解释器模式)
作者:webabcd
介绍
给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。
示例
有一个Message实体类,某个类对它的操作有Get()方法。现在要求用具有某一规则的中文语法来执行这个操作。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter
{
/**////
/// Message实体类
///
public class MessageModel
{
/**////
/// 构造函数
///
/// Message内容
/// Message发布时间
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/**////
/// Message内容
///
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/**////
/// Message发布时间
///
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter
{
/**////
/// Sql方式操作Message
///
public class SqlMessage
{
/**////
/// 获取Message
///
///
public static List Get()
{
List l = new List();
l.Add(new MessageModel("SQL方式获取Message", DateTime.Now));
return l;
}
}
}
Context
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter
{
/**////
/// Context
///
public class Context
{
private string _input;
private string _output;
/**////
/// 构造函数
///
/// 输入内容
public Context(string input)
{
this._input = input;
}
/**////
/// 输入内容
///
public string Input
{
get { return _input; }
set { _input = value; }
}
/**////
/// 输出内容
///
public string Output
{
get { return _output; }
set { _output = value; }
}
}
}
AbstractExpression
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter
{
/**////
/// 抽象公式(AbstractExpression)
///
public abstract class AbstractExpression
{
/**////
/// 解释Context的方法
///
/// context
public void Interpret(Context context)
{
if (String.IsNullOrEmpty(context.Input))
{
return;
}
context.Output += GetCSharp(context.Input);
}
/**////
/// 获得输入内容所对应的C#代码
///
/// source
///
private string GetCSharp(string source)
{
string csharp = "";
string word = "";
// 从输入内容中取得要解释的词
word = GetWord(source);
// 从字典中找到word所对应的C#代码
GetDictionary().TryGetValue(word, out csharp);
return csharp;
}
/**////
/// 从输入内容中取得要解释的词
///
/// source
///
public abstract string GetWord(string source);
/**////
/// 获取字典
///
///
public abstract Dictionary GetDictionary();
}
}
DatabaseExpression
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Pattern.Interpreter
{
/**////
/// 终端公式(TerminalExpression)分析与数据库相关的
///
public class DatabaseExpression : AbstractExpression
{
/**////
/// 从输入内容中取得要解释的词
///
/// source
///
public override string GetWord(string source)
{
MatchCollection mc;
Regex r = new Regex(@"\{(.*)\}");
mc = r.Matches(source);
return mc[0].Groups[1].Value;
}
/**////
/// 获取与数据库相关的字典
///
///
public override Dictionary GetDictionary()
{
Dictionary d = new Dictionary();
d.Add("数据库", "Sql");
return d;
}
}
}
ObjectExpression
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Pattern.Interpreter
{
/**////
/// 终端公式(TerminalExpression)分析与对象相关的
///
public class ObjectExpression : AbstractExpression
{
/**////
/// 从输入内容中取得要解释的词
///
/// source
///
public override string GetWord(string source)
{
MatchCollection mc;
Regex r = new Regex(@"\[(.*)\]");
mc = r.Matches(source);
return mc[0].Groups[1].Value;
}
/**////
/// 获取与对象相关的字典
///
///
public override Dictionary GetDictionary()
{
Dictionary d = new Dictionary();
d.Add("信息", "Message");
return d;
}
}
}
MethodExpression
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Pattern.Interpreter
{
/**////
/// 终端公式(TerminalExpression)分析与方法相关的
///
public class MethodExpression : AbstractExpression
{
/**////
/// 从输入内容中取得要解释的词
///
/// source
///
public override string GetWord(string source)
{
MatchCollection mc;
Regex r = new Regex(@"\((.*)\)");
mc = r.Matches(source);
return mc[0].Groups[1].Value;
}
/**////
/// 获取与方法相关的字典
///
///
public override Dictionary GetDictionary()
{
Dictionary d = new Dictionary();
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 l = new List();
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 = assembly.GetType("Pattern.Interpreter." + context.Output.Split('.')[0]).GetMethod(context.Output.Split('.')[1].Replace("()", ""));
object obj = method.Invoke(null, null);
List m = (List)obj;
Response.Write("中文语法:" + chinese);
Response.Write("
"); Response.Write("解释后的C#代码:" + context.Output); Response.Write("
"); Response.Write("执行结果:" + m[0].Message + " " + m[0].PublishTime.ToString()); } } 运行结果 中文语法:{数据库}[信息](获取) 解释后的C#代码:SqlMessage.Get() 执行结果:SQL方式获取Message 2007-5-1 8:48:07 OK [源码下载]
"); Response.Write("解释后的C#代码:" + context.Output); Response.Write("
"); Response.Write("执行结果:" + m[0].Message + " " + m[0].PublishTime.ToString()); } } 运行结果 中文语法:{数据库}[信息](获取) 解释后的C#代码:SqlMessage.Get() 执行结果:SQL方式获取Message 2007-5-1 8:48:07 OK [源码下载]
浙公网安备 33010602011771号