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

2020-09-01 14:04:02

本文实例讲述了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( )被分离在一个不同的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... 

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

<?phpheader("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面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

相关文章 大家在看