星空网 > 软件开发 > ASP.net

解释器模式和php实现

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

角色:
  环境角色:定义解释规则的全局信息。
  抽象解释器::定义了部分解释具体实现,封装了一些由具体解释器实现的接口。
  具体解释器(MusicNote):实现抽象解释器的接口,进行具体的解释执行。

UML图:

    解释器模式和php实现

代码实现:

<?php//环境角色,定义要解释的全局内容class Expression{  public $content;  function getContent(){    return $this->content;  }}//抽象解释器abstract class AbstractInterpreter{  abstract function interpret($content);}//具体解释器,实现抽象解释器的抽象方法class ChineseInterpreter extends AbstractInterpreter{  function interpret($content){    switch($content){      case '0': echo "没有人";break;      case "1": echo "一个人";break;      case "2": echo "二个人";break;      case "3": echo "三个人";break;      case "4": echo "四个人";break;      case "5": echo "五个人";break;      case "6": echo "六个人";break;      case "7": echo "七个人";break;      case "8": echo "八个人";break;      case "9": echo "九个人";break;      default:echo "其他";    }  }}class EnglishInterpterer extends AbstractInterpreter{  function interpret($content){     switch($content){      case '0': echo "This is nobody";break;      case "1": echo "This is one people";break;      case "2": echo "This is two people";break;      case "3": echo "This is three people";break;      case "4": echo "This is four people";break;      case "5": echo "This is five people";break;      case "6": echo "This is six people";break;      case "7": echo "This is seven people";break;      case "8": echo "This is eight people";break;      case "9": echo "This is nine people";break;      default:echo "others";    }  }}//封装好的对具体解释器的调用类,非解释器模式必须的角色class Interpreter{   private $interpreter;   private $content;   function __construct(){     $expression = new Expression();    $this->content = $expression->getContent();    if($content[0] == "Chinese"){       $this->interpreter = new ChineseInterpreter();     }else{       $this->interpreter = new EnglishInterpreter();     }   }   function execute(){     $this->interpreter->interpret($this->content);   }}//测试Expression::$content = array("Chinese",3,2,4,4,5);$interpreter = new Interpreter();$interpreter->execute();Expression::$content = array("English",3,2,4,4,5);$interpreter = new Interpreter();$interpreter->execute();?>

 

适用性:
  当有一个语言需要解释执行, 并且你可将该语言中的句子表示为一个抽象语法树时,可使用解释器模式。而当存在以下情况时该模式效果最好:
  该文法简单对于复杂的文法, 文法的类层次变得庞大而无法管理。此时语法分析程序生成器这样的工具是更好的选择。它们无需构建抽象语法树即可解释表达式, 这样可以节省空间而且还可能节省时间。
  效率不是一个关键问题最高效的解释器通常不是通过直接解释语法分析树实现的, 而是首先将它们转换成另一种形式。例如,正则表达式通常被转换成状态机。但即使在这种情况下, 转换器仍可用解释器模式实现, 该模式仍是有用的。

 




原标题:解释器模式和php实现

关键词:PHP

PHP
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

马里亚纳海运:https://www.goluckyvip.com/tag/92168.html
比雷埃夫斯海运:https://www.goluckyvip.com/tag/92169.html
罚款政策:https://www.goluckyvip.com/tag/9217.html
新加坡海运船运:https://www.goluckyvip.com/tag/92170.html
海运到新加坡海运:https://www.goluckyvip.com/tag/92172.html
到新加坡海运拼箱:https://www.goluckyvip.com/tag/92174.html
怪物在游轮上复活的电影 怪物在游轮上复活的电影叫什么:https://www.vstour.cn/a/411230.html
在线旅游如何选择更优惠的旅游产品?:https://www.vstour.cn/a/411231.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流