Interpreter Pattern(解释器模式)

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


作者: webabcd


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


示例
有一个Message实体类,某个类对它的操作有Get()方法。现在要求用具有某一规则的中文语法来执行这个操作。



MessageModel
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Pattern.Interpreter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Message实体类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class MessageModel
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="msg">Message内容</param>
ExpandedSubBlockEnd.gif        
/// <param name="pt">Message发布时间</param>

InBlock.gif        public MessageModel(string msg, DateTime pt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._message = msg;
InBlock.gif            
this._publishTime = pt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _message;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Message内容
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Message
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _message; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _message = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DateTime _publishTime;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Message发布时间
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public DateTime PublishTime
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _publishTime; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _publishTime = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

SqlMessage
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Pattern.Interpreter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Sql方式操作Message
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SqlMessage
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取Message
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static List<MessageModel> Get()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

Context
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Pattern.Interpreter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Context
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Context
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string _input;
InBlock.gif        
private string _output;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="input">输入内容</param>

InBlock.gif        public Context(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._input = input;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 输入内容
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Input
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _input; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _input = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 输出内容
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Output
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _output; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _output = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

AbstractExpression
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Pattern.Interpreter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 抽象公式(AbstractExpression)
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public abstract class AbstractExpression
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 解释Context的方法
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="context">context</param>

InBlock.gif        public void Interpret(Context context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (String.IsNullOrEmpty(context.Input))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            context.Output 
+= GetCSharp(context.Input);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得输入内容所对应的C#代码
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source">source</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private string GetCSharp(string source)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从输入内容中取得要解释的词
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source">source</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public abstract string GetWord(string source);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取字典
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public abstract Dictionary<stringstring> GetDictionary();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

DatabaseExpression
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
namespace  Pattern.Interpreter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 终端公式(TerminalExpression)分析与数据库相关的
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DatabaseExpression : AbstractExpression
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从输入内容中取得要解释的词
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source">source</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override string GetWord(string source)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取与数据库相关的字典
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override Dictionary<stringstring> GetDictionary()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dictionary
<stringstring> d = new Dictionary<stringstring>();
InBlock.gif
InBlock.gif            d.Add(
"数据库""Sql");
InBlock.gif
InBlock.gif            
return d;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

ObjectExpression
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
namespace  Pattern.Interpreter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 终端公式(TerminalExpression)分析与对象相关的
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ObjectExpression : AbstractExpression
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从输入内容中取得要解释的词
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source">source</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override string GetWord(string source)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取与对象相关的字典
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override Dictionary<stringstring> GetDictionary()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dictionary
<stringstring> d = new Dictionary<stringstring>();
InBlock.gif
InBlock.gif            d.Add(
"信息""Message");
InBlock.gif
InBlock.gif            
return d;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

MethodExpression
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
namespace  Pattern.Interpreter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 终端公式(TerminalExpression)分析与方法相关的
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public class MethodExpression : AbstractExpression
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从输入内容中取得要解释的词
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source">source</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>
InBlock.gif        public override string GetWord(string source)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取与方法相关的字典
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>
InBlock.gif        public override Dictionary<stringstring> GetDictionary()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dictionary
<stringstring> d = new Dictionary<stringstring>();
InBlock.gif
InBlock.gif            d.Add(
"获取"".Get()");
InBlock.gif
InBlock.gif            
return d;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


client
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
using  Microsoft.CSharp;
None.gif
using  System.Reflection;
None.gif
using  System.Text;
None.gif
using  System.Collections.Generic;
None.gif
None.gif
using  Pattern.Interpreter;
None.gif
None.gif
public  partial  class  Interpreter : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            exp.Interpret(context);
ExpandedSubBlockEnd.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());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

运行结果
中文语法:{数据库}[信息](获取)
解释后的C#代码:SqlMessage.Get()
执行结果:SQL方式获取Message 2007-5-1 8:48:07

转载于:https://www.cnblogs.com/piwxsz/archive/2007/11/08/953098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值