zoukankan      html  css  js  c++  java
  • 4.3 Interpreter(解释器)

    【返回目录】

    其实描述解释器工作原理的例子在现实生活中有很多,我觉得用计算机程序语言来解释可能会使理解的门槛稍稍高一点儿,不妨我们就用日常语言来打比方吧,毕竟这更容易让人准确地理解什么是解释器。

    如果你对一个女孩子说“我爱你”这三个字,她会有什么反应呢?通常是三种:1,她等你这句话很久了,脸红、心跳;2,她一直把你当好朋友看待或觉得你很陌生,不来电、可能有点儿感动;3,她一向对你印象不好,反感、逃避。

    至于她如何响应你的Request,是封装在她内部的一套机制来决定的,由于信息隐藏原则,作为调用对象的你,往往不清楚其细节,正所谓“女孩的心思你别猜,你猜了也白猜”。那么,她是如何根据你的请求来做出相应的呢?这就是解释器要做的事情,解释器适合于发生频率比较高的特定类型的问题,比如说话,我们每天都在说话,大脑时刻要处理别人对我们说的话以及我们自己心里的自言自语,所以里用解释器是很有必要的,解释器就是对语言的翻译过程。你将“我爱你”这条信息发送给女孩,她就会里用解释器来解析这句话的意思,根据常用语法,我们假设有三个子解释器:主语解释器、谓语解释器和宾语解释器。她会利用这些解释器来将所获得的信息解释成其内部子系统可以理解的信息以便做出相应的回应。当然,只要解释器内部的功能强大,她完全可以解释更多更复杂的信息,比如:“刚才那人说他爱你是骗你的,其实我才是真心对你好的!”

       1: using System;
       2: using System.Collections;
       3:  
       4: namespace Autumoon.DesignPatterns.Interpreter
       5: {
       6:     public class Sentence
       7:     {
       8:     }
       9:  
      10:     public abstract class AbstractExpression
      11:     {
      12:         abstract public void Interpret(Sentence sentence);
      13:     }
      14:  
      15:     public class SubjectExpression : AbstractExpression
      16:     {
      17:         override public void Interpret(Sentence sentence)
      18:         {
      19:             // Do something you want.
      20:         }
      21:     }
      22:  
      23:     public class PredicateExpression : AbstractExpression
      24:     {
      25:         override public void Interpret(Sentence sentence)
      26:         {
      27:             // Do something you want.
      28:         }
      29:     }
      30:  
      31:     public class AccusativeExpression : AbstractExpression
      32:     {
      33:         public override void Interpret(Sentence sentence)
      34:         {
      35:             // Do something you want.
      36:         }
      37:     }
      38: }

    不信,你现在就大胆地表白:

       1: static void Main(string[] args)
       2: {
       3:     #region Interpreter
       4:     Sentence sentence = new Sentence();
       5:  
       6:     ArrayList tree = new ArrayList();
       7:     tree.Add(new SubjectExpression());
       8:     tree.Add(new PredicateExpression());
       9:     tree.Add(new AccusativeExpression());
      10:  
      11:     foreach (AbstractExpression expression in tree)
      12:     {
      13:         expression.Interpret(sentence);
      14:     }
      15:     #endregion
      16:  
      17:     Console.ReadLine();
      18: }

    Interpreter

  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/Autumoon/p/1077851.html
Copyright © 2011-2022 走看看