PHP設(shè)計(jì)模式之解釋器(Interpreter)模式入門與應(yīng)用詳解
本文實(shí)例講述了PHP設(shè)計(jì)模式之解釋器(Interpreter)模式。分享給大家供大家參考,具體如下:
解釋器模式,它是什么呢?
意思就是,給定一個(gè)語言, 定義它的文法的一種表示,并定義一個(gè)解釋器,該解釋器使用該表示來解釋語言中的句子,這是最實(shí)在的一種說法。
我們還可以理解為它是用于分析一個(gè)實(shí)體的關(guān)鍵元素,并且針對(duì)每個(gè)元素提供自己的解釋或相應(yīng)動(dòng)作。解釋器模式非常常用,比如PHP的模板引擎 就是非常常見的一種解釋器模。
咱來看一個(gè)網(wǎng)上找的最簡單的實(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è)模板類,簡單的模板類,標(biāo)題為:<!--{Hello World}-->"; $template = new template; echo $template->run($str);
通過上述實(shí)例,大家對(duì)于解釋器模式肯定有了自己的一個(gè)簡單理解,我們接下來就看下這個(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è)抽象的語法樹,除了復(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é)果,使解釋過程大大簡化。
但是,對(duì)于簡單的語法,解釋器添加一個(gè)規(guī)則就象添加一個(gè)類那樣容易,但解釋器沒有解決從具體表現(xiàn)形式到抽象語法樹的轉(zhuǎn)換,這是由其它服務(wù)完成的。
解釋器模式旨在為一個(gè)簡單的抽象表達(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)一步簡化的表達(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
好啦,本次記錄就到這里了。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php設(shè)計(jì)模式 Builder(建造者模式)
- 學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)建造者模式
- PHP設(shè)計(jì)模式之建造者模式定義與用法簡單示例
- PHP設(shè)計(jì)模式之適配器模式(Adapter)原理與用法詳解
- PHP設(shè)計(jì)模式之策略模式(Strategy)入門與應(yīng)用案例詳解
- PHP設(shè)計(jì)模式之單例模式入門與應(yīng)用詳解
- PHP設(shè)計(jì)模式之觀察者模式入門與應(yīng)用案例詳解
- PHP設(shè)計(jì)模式之中介者模式(Mediator Pattern)入門與應(yīng)用案例詳解
- PHP設(shè)計(jì)模式之迭代器(Iterator)模式入門與應(yīng)用詳解
- PHP設(shè)計(jì)模式之觀察者模式(Observer)詳細(xì)介紹和代碼實(shí)例
- PHP經(jīng)典面試題之設(shè)計(jì)模式(經(jīng)常遇到)
- PHP設(shè)計(jì)模式之建造者模式(Builder)原理與用法案例詳解
相關(guān)文章
php 論壇采集程序 模擬登陸,抓取頁面 實(shí)現(xiàn)代碼
php 論壇采集程序 模擬登陸,抓取頁面 實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-07-07php網(wǎng)上商城購物車設(shè)計(jì)代碼分享
我們要做的是一個(gè)可以包含促銷活動(dòng)的購物車,所以比較其他簡單的購物車,會(huì)稍微復(fù)雜一點(diǎn)。(用的是PHP的zend framework框架)2012-02-02PHP編寫學(xué)校網(wǎng)站上新生注冊(cè)登陸程序的實(shí)例分享
這篇文章主要介紹了使用PHP編寫學(xué)校網(wǎng)站上新生注冊(cè)登陸程序的實(shí)例分享,只包括簡單的數(shù)據(jù)庫信息錄入和查詢等步驟的一些關(guān)鍵點(diǎn),需要的朋友可以參考下2016-03-03php數(shù)組查找函數(shù)in_array()、array_search()、array_key_exists()使用實(shí)例
這篇文章主要介紹了php數(shù)組查找函數(shù)in_array()、array_search()、array_key_exists()使用實(shí)例,需要的朋友可以參考下2014-04-04PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼
這篇文章主要介紹了PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04