首页 > 作文

PHP设计模式之解释器(Interpreter)模式入门与应用详解

更新时间:2023-04-08 04:37:10 阅读: 评论:0

本文实例讲述了php设计模式之解释器(interpreter)模式。分享给大家供大家参考,具体如下:

解释器模式,它是什么呢?

意思就是,给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子,这是最实在的一种说法。

我们还可以理解为它是用于分析一个实体的关键元素,并且针对每个元素提供自己的解释或相应动作。解释器模式非常常用,比如php的模板引擎 就是非常常见的一种解释器模。

咱来看一个网上找的最简单的实例:

<?php//解释器模式 用于分析一个实体的关键元素,并且针对每个元素提供自己的解释或相应动作//解释器模式非常常用,比如php的模板引擎 就是非常常见的一种解释器模式class template {  private $left = '<!--{'; private $right = '}-->';  public function run($str) { return $this->init($str, $this->left, $this->right); }  /** * 模板驱动-默认的驱动 * @param string $str 模板文件数据 * @return string */ private function init($str, $left, $right) { $pattern = array('/'.$left.'/', '/'.$right.'/'); $replacement = array('', ''); return preg_replace($pattern, $replacement, $str); }}$str = "这是一个模板类,简单的模板类,标题为:<!--{hello world}-->";$template = new template;echo $template->run($str);

通过上述实例,大家对于解释器模式肯定有了自己的一个简单理解,我们接下来就看下这个解释器所包含的角色:

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

完事,咱在网上看的,对于解释器(interpreter)模式,还有另外一种说法,那就是它包括一个具有复合类分层结构的文法表现,规则是映射到类,跟随在文法后面的表达式可以被转换成一个抽象的语法树,除了复合模式的实例对象图外,没有别的内容。

树是一个抽象的名词,因为实际上大多数时候它是一个表达式的抽象表现,它忽略了可能有一个字符串,也可能有一个数据结构的具体表达式,(例如,在php中,“a”和“\x41”是相同抽象字面值的不同具体表现),通过逻辑规则解耦结果,使解释过程大大简化。

但是,对于简单的语法,解释器添加一个规则就象添加一个类那样容易,但解释器没有解决从具体表现形式到抽象语法树的转换,这是由其它服务完成的。

解释器模式旨在为一个简单的抽象表达式(abstractexpression)方法(解释器操作)实现利用复合分层结构,解释器操作的参数通常统称为上下文,对于给定的一个方法,它们通常被计算值代替,或它们对某些操作可能不存在。

同样,当包含一个解释器时,复合模式的叶子和容器参与者名称会不一样,这些名称反映了它们所扮演的角色:终结符(terminal)或非终结符(nonterminal)表达式。

来看下参与者:

◆客户端(client):使用解释操作。
◆抽象表达式(abstractexpression):基于一个表达式树抽象。
◆非终结符表达式(nonterminalexpression):递归地包含其它抽象表达式(abstractexpression实例)的表达式。
◆终结符表达式(terminalexpression):不能够进一步简化的表达式。

我们来看下《设计模式》一书针对这个模式提供的一个扩展示例,是一个网友使用数学表达式替换布尔表达式重新改造了一下,因此这个例子解决了一个数学表达式的展现,它的evaluate( )被分离在一个不同的concreteexpressio数学质量分析n类中,如下:

/**  * abstractexpression. all implementations of this interface  * are concreteexpressions.  */ interface mathexpression {  /**   * calculates the value assumed by the expression.   * note that $values is pasd to all expression but it   * is ud by variable only. this is required to abstract   * away the tree structure.   */  public function evaluate(array $values); }  /**  * a terminal expression which is a literal value.  */ class literal implements mathexpression {  private $_value;   public function __construct($value)  {   $this->_value = $value;  }   public function evaluate(array $values)  {   return $this->_value;  } }  /*暑期兼职*  * a terminal expression which reprents a variable.  */ class variable implements mathexpression {  private $_letter;   public function __construct($letter)  {   $this->_letter = $letter;  }   public function evaluate(array $values)  {   return $values[$this->_letter];  } }  /**  * nonterminal expression.  */ class sum implements mathexpression {  private $_a;  private $_b;   public function __construct(mathexpression $a, mathexpression $b)  {   $this->_a = $a;   $this->_b = $b;  }   public function evaluate(array $values)  {   return $this->_a->evaluate($values) + $this->_b->evaluate($values);  } }  /**  * nonterminal expression.  */ class product implements mathexpression {  private $_a;  private $_b;   public function __construct(mathexpression $a, mathexpression $b)  {   $this->_a = $a;   $this->_b = $b;  }   public function evaluate(array $values)  {   return $this->_a->evaluate($values) * $this->_b->evaluate($values);  } }  // 10(a + 3) $expression = new product(new literal(10), new sum(new variable('a'), new literal(3))); echo $expression->evaluate(array('a' => 4)), "\n"; // adding new rules to the grammar is easy: // e.g. power, subtraction... // thanks to the composite, manipulation is even simpler: // we could add substitute($letter, mathexpression $expr) // to the interface... 

咱最后再分享一个实例,如下:

<?phpheader("content-type:text/html;chart=utf-8"); //环境角色,定义要解释的全局内容class expression{ public $content; funct特殊角的三角函数值ion getcontent(){  return $this->content; }} //抽象解释器abstract class abstractinterpreter{ abstract function interpret($content);} //具体解释器,实现抽象解释器的抽象方法class chineinterpreter extends abstractinterpreter{ function interpret($content){  for($i=1;$i<count($content);$i++){   switch($content[$i]){   ca '0': echo "没有人<br>";break;   ca "1": echo "一个人<br>";break;   ca "2": echo "二个人<br>";break;   ca "3": echo "三个人<br>";break;   ca "4": echo "四个人<br>";break;   ca "5": echo "五个人<br>";break;   ca "6": echo "六个人<br>";break;   ca "7": echo "七个人<br>";break;   ca "8": echo "八个人<br>";break;   ca "9": echo "九个人<br>";break;   default:echo "其他";   }  } }}class englishinterpreter extends abstractinterpreter{ function interpret($content){  for($i=1;$i<count($content);$i++){    switch($content[$i]){    ca '0': echo "this is nobody<br>";break;    ca "1": echo "this is one people<br>";break;    ca "2": echo "this is two people<br>";break;    ca "3": echo "this is three people<br>";break;    ca "4": echo "this is four people<br>";break;    ca "5": echo "this is five people<br>";break;    ca "6": echo "this is six people<br>";break;    ca "7": echo "this is ven people<br>";break;观赏凤梨    ca "8": echo "this is eight people<br>";break;    ca "9": echo "this is nine people<br>";break;    default:echo "others";   }  } }} //封装好的对具体解释器的调用类,非解释器模式必须的角色class interpreter{  private $interpreter;  private $content;  function __construct($expression){  $this->content = $expression->getcontent();  if($this->content[0] == "chine"){    $this->interpreter = new chineinterpreter();   }el我{    $this->interpreter = new englishinterpreter();   }  }  function execute(){   $this->interpreter->interpret($this->content);  }} //测试$expression = new expression();$expression->content = array("chine",3,2,4,4,5);$interpreter = new interpreter($expression);$interpreter->execute(); $expression = new expression();$expression->content = array("english",1,2,3,0,0);$interpreter = new interpreter($expression);$interpreter->execute();?>

结果:

三个人
二个人
四个人
四个人
五个人
this is one people
this is two people
this is three people
this is nobody
this is nobody

好啦,本次记录就到这里了。

本文发布于:2023-04-08 04:37:08,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/311992be4f00967d78d020aadbc0a573.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP设计模式之解释器(Interpreter)模式入门与应用详解.doc

本文 PDF 下载地址:PHP设计模式之解释器(Interpreter)模式入门与应用详解.pdf

标签:表达式   抽象   模式   自己的
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图