惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
腾讯CDC
人人都是产品经理
人人都是产品经理
小众软件
小众软件
V
Visual Studio Blog
S
Secure Thoughts
J
Java Code Geeks
V
V2EX
量子位
The Hacker News
The Hacker News
酷 壳 – CoolShell
酷 壳 – CoolShell
Security Latest
Security Latest
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tailwind CSS Blog
Cloudbric
Cloudbric
S
SegmentFault 最新的问题
AI
AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
T
Tenable Blog
S
Security @ Cisco Blogs
月光博客
月光博客
雷峰网
雷峰网
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
Attack and Defense Labs
Attack and Defense Labs
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
有赞技术团队
有赞技术团队
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
TaoSecurity Blog
TaoSecurity Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Cloudflare Blog
K
Kaspersky official blog

博客园 - 宽田

SQLite在.NET中自适应32位/64位系统 OWIN是什么? 好的用户界面-界面设计的一些技巧 Android模拟器连接本李服务器localhost win7 64位 VS2010调试提示“ORA-12154: TNS: 无法解析指定的连接标识符”的解决方法 Js 验证中文字符长度 Javascript技巧 访问者模式(Visitor) 职责链模式(Chain of Responsibility) Web Service 中返回DataSet结果大小改进 通用数据链接(UDL)的用法 ORACLE WITH AS 用法 Oracle REGEXP_INSTR 用法 C# 实现自定义事件 错误:200, Stream not found, NetStream.Play.StreamNotFound, clip: '[Clip] GetCallbackEventReference(客户端调用服务器端)的用法 wcf Svcutil用法 IIS 增加net.tcp绑定 客户端字符集NLS_LANG
解释器模式(interpreter):
宽田 · 2013-05-17 · via 博客园 - 宽田

解释器模式(Interpreter):

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

结构图:

2009-2-12-2

示例代码:

public class InterpreterMode
    {
        public void Main()
        {
            InterpreterContext context = new InterpreterContext();
            IList<AbstractExpression> list = new List<AbstractExpression>();
            list.Add(new TerminalExpression());
            list.Add(new NonterminalExpression());
            list.Add(new TerminalExpression());
            list.Add(new TerminalExpression());
            foreach (AbstractExpression item in list)
            {
                item.Interpret(context);
            }
        }
    }

    public class InterpreterContext
    {
        public string Input { get; set; }
        public string OutPut { get; set; }
    }

    /// <summary>
    /// 抽象解释操作
    /// </summary>
    public abstract class AbstractExpression
    {
        public abstract void Interpret(InterpreterContext context);
    }

    /// <summary>
    /// 终端解释
    /// </summary>
    public class TerminalExpression : AbstractExpression
    {
        public override void Interpret(InterpreterContext context)
        {
            Console.WriteLine("终端解释器");
        }
    }

    public class NonterminalExpression : AbstractExpression
    {
        public override void Interpret(InterpreterContext context)
        {
            Console.WriteLine("非终端解释器");
        }
    }

优点:

     用了解释器模式,就意味着可以很容易地改变和扩展文法,因为该模式使用类来表示文法规则,你可使用继承来来改变或扩展文法。也比较容易实现文法,因为定义抽象语法树中各个节点的类的实现大体类似,这些类都易于直接编写。

缺点:

    解释器模式为文法中的每一条规则至少定义了一个类,因此包含请多规则的文法可能难以管理和维护。建议当文法非常复杂时,使用其它的技术如语法分析程序或编译器生成器来处理。

适用性:

     解释器模式需要解决的是,如果一种特定类型的问题发生的频率足够高,那么可能将该问题的各个实例表述为一个简单语言中的句子。该解释器通过解释这些句子来解决该问题。

     当有一个语言需要解释执行,并且你可以将该语言中的句子表示为一个抽象语法树时,可使用解释器模式。