您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)PHP中怎么實(shí)現(xiàn)解釋器模式,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
解釋器模式,它是什么呢?
意思就是,給定一個(gè)語(yǔ)言, 定義它的文法的一種表示,并定義一個(gè)解釋器,該解釋器使用該表示來解釋語(yǔ)言中的句子,這是最實(shí)在的一種說法。
我們還可以理解為它是用于分析一個(gè)實(shí)體的關(guān)鍵元素,并且針對(duì)每個(gè)元素提供自己的解釋或相應(yīng)動(dòng)作。解釋器模式非常常用,比如PHP的模板引擎 就是非常常見的一種解釋器模。
咱來看一個(gè)網(wǎng)上找的最簡(jiǎn)單的實(shí)例:
<?php //解釋器模式 用于分析一個(gè)實(shí)體的關(guān)鍵元素,并且針對(duì)每個(gè)元素提供自己的解釋或相應(yīng)動(dòng)作 //解釋器模式非常常用,比如PHP的模板引擎 就是非常常見的一種解釋器模式 class template { private $left = '<!--{'; private $right = '}-->'; public function run($str) { return $this->init($str, $this->left, $this->right); } /** * 模板驅(qū)動(dòng)-默認(rèn)的驅(qū)動(dòng) * @param string $str 模板文件數(shù)據(jù) * @return string */ private function init($str, $left, $right) { $pattern = array('/'.$left.'/', '/'.$right.'/'); $replacement = array('', ''); return preg_replace($pattern, $replacement, $str); } } $str = "這是一個(gè)模板類,簡(jiǎn)單的模板類,標(biāo)題為:<!--{Hello World}-->"; $template = new template; echo $template->run($str);
通過上述實(shí)例,大家對(duì)于解釋器模式肯定有了自己的一個(gè)簡(jiǎn)單理解,我們接下來就看下這個(gè)解釋器所包含的角色:
環(huán)境角色:定義解釋規(guī)則的全局信息。
抽象解釋器::定義了部分解釋具體實(shí)現(xiàn),封裝了一些由具體解釋器實(shí)現(xiàn)的接口。
具體解釋器(MusicNote):實(shí)現(xiàn)抽象解釋器的接口,進(jìn)行具體的解釋執(zhí)行。
完事,咱在網(wǎng)上看的,對(duì)于解釋器(Interpreter)模式,還有另外一種說法,那就是它包括一個(gè)具有復(fù)合類分層結(jié)構(gòu)的文法表現(xiàn),規(guī)則是映射到類,跟隨在文法后面的表達(dá)式可以被轉(zhuǎn)換成一個(gè)抽象的語(yǔ)法樹,除了復(fù)合模式的實(shí)例對(duì)象圖外,沒有別的內(nèi)容。
樹是一個(gè)抽象的名詞,因?yàn)閷?shí)際上大多數(shù)時(shí)候它是一個(gè)表達(dá)式的抽象表現(xiàn),它忽略了可能有一個(gè)字符串,也可能有一個(gè)數(shù)據(jù)結(jié)構(gòu)的具體表達(dá)式,(例如,在PHP中,“A”和“\x41”是相同抽象字面值的不同具體表現(xiàn)),通過邏輯規(guī)則解耦結(jié)果,使解釋過程大大簡(jiǎn)化。
但是,對(duì)于簡(jiǎn)單的語(yǔ)法,解釋器添加一個(gè)規(guī)則就象添加一個(gè)類那樣容易,但解釋器沒有解決從具體表現(xiàn)形式到抽象語(yǔ)法樹的轉(zhuǎn)換,這是由其它服務(wù)完成的。
解釋器模式旨在為一個(gè)簡(jiǎn)單的抽象表達(dá)式(AbstractExpression)方法(解釋器操作)實(shí)現(xiàn)利用復(fù)合分層結(jié)構(gòu),解釋器操作的參數(shù)通常統(tǒng)稱為上下文,對(duì)于給定的一個(gè)方法,它們通常被計(jì)算值代替,或它們對(duì)某些操作可能不存在。
同樣,當(dāng)包含一個(gè)解釋器時(shí),復(fù)合模式的葉子和容器參與者名稱會(huì)不一樣,這些名稱反映了它們所扮演的角色:終結(jié)符(terminal)或非終結(jié)符(nonterminal)表達(dá)式。
來看下參與者:
◆客戶端(Client):使用解釋操作。
◆抽象表達(dá)式(AbstractExpression):基于一個(gè)表達(dá)式樹抽象。
◆非終結(jié)符表達(dá)式(NonTerminalExpression):遞歸地包含其它抽象表達(dá)式(AbstractExpression實(shí)例)的表達(dá)式。
◆終結(jié)符表達(dá)式(TerminalExpression):不能夠進(jìn)一步簡(jiǎn)化的表達(dá)式。
我們來看下《設(shè)計(jì)模式》一書針對(duì)這個(gè)模式提供的一個(gè)擴(kuò)展示例,是一個(gè)網(wǎng)友使用數(shù)學(xué)表達(dá)式替換布爾表達(dá)式重新改造了一下,因此這個(gè)例子解決了一個(gè)數(shù)學(xué)表達(dá)式的展現(xiàn),它的evaluate( )被分離在一個(gè)不同的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...
咱最后再分享一個(gè)實(shí)例,如下:
<?php header("Content-type:text/html;Charset=utf-8"); //環(huán)境角色,定義要解釋的全局內(nèi)容 class Expression{ public $content; function getContent(){ return $this->content; } } //抽象解釋器 abstract class AbstractInterpreter{ abstract function interpret($content); } //具體解釋器,實(shí)現(xiàn)抽象解釋器的抽象方法 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 "一個(gè)人<br>";break; case "2": echo "二個(gè)人<br>";break; case "3": echo "三個(gè)人<br>";break; case "4": echo "四個(gè)人<br>";break; case "5": echo "五個(gè)人<br>";break; case "6": echo "六個(gè)人<br>";break; case "7": echo "七個(gè)人<br>";break; case "8": echo "八個(gè)人<br>";break; case "9": echo "九個(gè)人<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"; } } } } //封裝好的對(duì)具體解釋器的調(diào)用類,非解釋器模式必須的角色 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); } } //測(cè)試 $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(); ?>
結(jié)果:
三個(gè)人
二個(gè)人
四個(gè)人
四個(gè)人
五個(gè)人
This is one people
This is two people
This is three people
This is nobody
This is nobody
看完上述內(nèi)容,你們對(duì)PHP中怎么實(shí)現(xiàn)解釋器模式有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。