加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php解释器模式怎么理解?解释器模式示例详解

发布时间:2021-12-30 18:47:26 所属栏目:PHP教程 来源:互联网
导读:PHP设计模式是PHP学习的一个重要的内容,我们知道PHP设计模式有很多,这篇文主要给大家介绍的是关于PHP解释器模式的原理和使用等相关内容,对新手学习和理解解释器模式有一定的参考价值,感兴趣的朋友可以看看。 解释器模式,它是什么呢?意思就是,给定一
     PHP设计模式是PHP学习的一个重要的内容,我们知道PHP设计模式有很多,这篇文主要给大家介绍的是关于PHP解释器模式的原理和使用等相关内容,对新手学习和理解解释器模式有一定的参考价值,感兴趣的朋友可以看看。
 
       解释器模式,它是什么呢?意思就是,给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子,这是最实在的一种说法。
 
       我们还可以理解为它是用于分析一个实体的关键元素,并且针对每个元素提供自己的解释或相应动作。解释器模式非常常用,比如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( )被分离在一个不同的ConcreteExpression类中,如下:
 
/**
 * AbstractExpression. All implementations of this interface
 * are ConcreteExpressions.
 */
interface MathExpression
{
 /**
  * Calculates the value assumed by the expression.
  * Note that $values is passed to all expression but it
  * is used 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 represents 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...
 
       咱最后再分享一个实例,如下:
 
<?php
header("Content-type:text/html;Charset=utf-8");
 
//环境角色,定义要解释的全局内容
class Expression{
 public $content;
 function getContent(){
  return $this->content;
 }
}
 
//抽象解释器
abstract class AbstractInterpreter{
 abstract function interpret($content);
}
 
//具体解释器,实现抽象解释器的抽象方法
class ChineseInterpreter extends AbstractInterpreter{
 function interpret($content){
  for($i=1;$i<count($content);$i++){
   switch($content[$i]){
   case '0': echo "没有人<br>";break;
   case "1": echo "一个人<br>";break;
   case "2": echo "二个人<br>";break;
   case "3": echo "三个人<br>";break;
   case "4": echo "四个人<br>";break;
   case "5": echo "五个人<br>";break;
   case "6": echo "六个人<br>";break;
   case "7": echo "七个人<br>";break;
   case "8": echo "八个人<br>";break;
   case "9": echo "九个人<br>";break;
   default:echo "其他";
   }
  }
 }
}
class EnglishInterpreter extends AbstractInterpreter{
 function interpret($content){
  for($i=1;$i<count($content);$i++){
    switch($content[$i]){
    case '0': echo "This is nobody<br>";break;
    case "1": echo "This is one people<br>";break;
    case "2": echo "This is two people<br>";break;
    case "3": echo "This is three people<br>";break;
    case "4": echo "This is four people<br>";break;
    case "5": echo "This is five people<br>";break;
    case "6": echo "This is six people<br>";break;
    case "7": echo "This is seven people<br>";break;
    case "8": echo "This is eight people<br>";break;
    case "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] == "Chinese"){
    $this->interpreter = new ChineseInterpreter();
   }else{
    $this->interpreter = new EnglishInterpreter();
   }
  }
  function execute(){
   $this->interpreter->interpret($this->content);
  }
}
 
//测试
$expression = new Expression();
$expression->content = array("Chinese",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
 
       好啦,本次记录就到这里了。
 
       关于php解释器模式的相关介绍就到这,现在大家对于解释器模式的概念、原理、基本应用等等应该都有一定的了解了吧,希望大家阅读完这篇文章能有所收获,想要了解更多php解释器模式内容大家可以继续关注其他文章。

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读