C#设计模式之Interpreter
来源:互联网 发布:电子招投标系统源码 编辑:程序博客网 时间:2024/04/28 01:49
名称:Interpreter
结构:
意图:
给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
适用性 :
- 当有一个语言需要解释执行, 并且你可将该语言中的句子表示为一个抽象语法树时,可使用解释器模式。而当存在以下情况时该模式效果最好:
- 该文法简单对于复杂的文法, 文法的类层次变得庞大而无法管理。此时语法分析程序生成器这样的工具是更好的选择。它们无需构建抽象语法树即可解释表达式, 这样可以节省空间而且还可能节省时间。
- 效率不是一个关键问题最高效的解释器通常不是通过直接解释语法分析树实现的, 而是首先将它们转换成另一种形式。例如,正则表达式通常被转换成状态机。但即使在这种情况下, 转换器仍可用解释器模式实现, 该模式仍是有用的。
示例代码:
// Interpreter
namespace Interpreter_DesignPattern
{
using System;
using System.Collections;
class Context
{
}
abstract class AbstractExpression
{
abstract public void Interpret(Context c);
}
// class for terminal symbol
class TerminalExpression : AbstractExpression
{
override public void Interpret(Context c)
{
}
}
// class for grammar rule (one per rule needed)
class NonterminalExpression : AbstractExpression
{
override public void Interpret(Context c)
{
}
}
// to extend grammar, just add other NonterminalExpression classes
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
Context c = new Context();
ArrayList l = new ArrayList(); //really need a tree here!
// build up context information
// . . .
// Populate abstract syntax tree with data
l.Add(new TerminalExpression());
l.Add(new NonterminalExpression());
// interpret
foreach (AbstractExpression exp in l)
{
exp.Interpret(c);
}
return 0;
}
}
}
namespace Interpreter_DesignPattern
{
using System;
using System.Collections;
class Context
{
}
abstract class AbstractExpression
{
abstract public void Interpret(Context c);
}
// class for terminal symbol
class TerminalExpression : AbstractExpression
{
override public void Interpret(Context c)
{
}
}
// class for grammar rule (one per rule needed)
class NonterminalExpression : AbstractExpression
{
override public void Interpret(Context c)
{
}
}
// to extend grammar, just add other NonterminalExpression classes
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
Context c = new Context();
ArrayList l = new ArrayList(); //really need a tree here!
// build up context information
// . . .
// Populate abstract syntax tree with data
l.Add(new TerminalExpression());
l.Add(new NonterminalExpression());
// interpret
foreach (AbstractExpression exp in l)
{
exp.Interpret(c);
}
return 0;
}
}
}
- C#设计模式之Interpreter
- 【设计模式】之六Interpreter
- 设计模式之Interpreter解释器模式
- 设计模式之解释器模式,interpreter
- 设计模式之解释器模式(interpreter)
- 设计模式之Interpreter模式(笔记)
- 设计模式之:解释器模式(Interpreter)
- 设计模式之解释器模式(Interpreter)
- 设计模式之Interpreter(解释器)
- 设计模式之Interpreter(解释器)
- 设计模式之Interpreter(解释器)
- 设计模式之Interpreter(解释器)
- 设计模式之 interpreter和iterator
- GOF设计模式之INTERPRETER(解释器)
- Interpreter设计模式
- Interpreter设计模式
- interpreter 设计模式
- JAVA设计模式-Interpreter
- GSM MODEM发PUSH/MMS 的具体实现.
- 利用userData实现客户端保存表单数据
- Spb.Diary.v2.2.1.Multilanguage.XScale.WM2003.WM5
- 给Q的一封邮件
- C#设计模式之Prototype
- C#设计模式之Interpreter
- Linux 下获得系统时间
- 希望你会爱上我的blog
- jsp 自定义分页标签
- 軟件別 BT
- PB 通过存储程序实现得到两个日期之前的差的秒
- 关于hibernate.properties和hibernate.cfg.xml的分析
- 针对近期好多朋友同事咨询如何下载BT资源。本人推出终级原创文章!
- ddd