PHP設(shè)計模式之解釋器(Interpreter)模式入門與應(yīng)用詳解
本文實例講述了PHP設(shè)計模式之解釋器(Interpreter)模式。分享給大家供大家參考,具體如下:
解釋器模式,它是什么呢?
意思就是,給定一個語言, 定義它的文法的一種表示,并定義一個解釋器,該解釋器使用該表示來解釋語言中的句子,這是最實在的一種說法。
我們還可以理解為它是用于分析一個實體的關(guān)鍵元素,并且針對每個元素提供自己的解釋或相應(yīng)動作。解釋器模式非常常用,比如PHP的模板引擎 就是非常常見的一種解釋器模。
咱來看一個網(wǎng)上找的最簡單的實例:
<?php //解釋器模式 用于分析一個實體的關(guān)鍵元素,并且針對每個元素提供自己的解釋或相應(yīng)動作 //解釋器模式非常常用,比如PHP的模板引擎 就是非常常見的一種解釋器模式 class template { private $left = '<!--{'; private $right = '}-->'; public function run($str) { return $this->init($str, $this->left, $this->right); } /** * 模板驅(qū)動-默認的驅(qū)動 * @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 = "這是一個模板類,簡單的模板類,標題為:<!--{Hello World}-->"; $template = new template; echo $template->run($str);
通過上述實例,大家對于解釋器模式肯定有了自己的一個簡單理解,我們接下來就看下這個解釋器所包含的角色:
- 環(huán)境角色:定義解釋規(guī)則的全局信息。
- 抽象解釋器::定義了部分解釋具體實現(xiàn),封裝了一些由具體解釋器實現(xiàn)的接口。
- 具體解釋器(MusicNote):實現(xiàn)抽象解釋器的接口,進行具體的解釋執(zhí)行。
完事,咱在網(wǎng)上看的,對于解釋器(Interpreter)模式,還有另外一種說法,那就是它包括一個具有復合類分層結(jié)構(gòu)的文法表現(xiàn),規(guī)則是映射到類,跟隨在文法后面的表達式可以被轉(zhuǎn)換成一個抽象的語法樹,除了復合模式的實例對象圖外,沒有別的內(nèi)容。
樹是一個抽象的名詞,因為實際上大多數(shù)時候它是一個表達式的抽象表現(xiàn),它忽略了可能有一個字符串,也可能有一個數(shù)據(jù)結(jié)構(gòu)的具體表達式,(例如,在PHP中,“A”和“\x41”是相同抽象字面值的不同具體表現(xiàn)),通過邏輯規(guī)則解耦結(jié)果,使解釋過程大大簡化。
但是,對于簡單的語法,解釋器添加一個規(guī)則就象添加一個類那樣容易,但解釋器沒有解決從具體表現(xiàn)形式到抽象語法樹的轉(zhuǎn)換,這是由其它服務(wù)完成的。
解釋器模式旨在為一個簡單的抽象表達式(AbstractExpression)方法(解釋器操作)實現(xiàn)利用復合分層結(jié)構(gòu),解釋器操作的參數(shù)通常統(tǒng)稱為上下文,對于給定的一個方法,它們通常被計算值代替,或它們對某些操作可能不存在。
同樣,當包含一個解釋器時,復合模式的葉子和容器參與者名稱會不一樣,這些名稱反映了它們所扮演的角色:終結(jié)符(terminal)或非終結(jié)符(nonterminal)表達式。
來看下參與者:
◆客戶端(Client):使用解釋操作。
◆抽象表達式(AbstractExpression):基于一個表達式樹抽象。
◆非終結(jié)符表達式(NonTerminalExpression):遞歸地包含其它抽象表達式(AbstractExpression實例)的表達式。
◆終結(jié)符表達式(TerminalExpression):不能夠進一步簡化的表達式。
我們來看下《設(shè)計模式》一書針對這個模式提供的一個擴展示例,是一個網(wǎng)友使用數(shù)學表達式替換布爾表達式重新改造了一下,因此這個例子解決了一個數(shù)學表達式的展現(xiàn),它的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"); //環(huán)境角色,定義要解釋的全局內(nèi)容 class Expression{ public $content; function getContent(){ return $this->content; } } //抽象解釋器 abstract class AbstractInterpreter{ abstract function interpret($content); } //具體解釋器,實現(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 "一個人<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"; } } } } //封裝好的對具體解釋器的調(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); } } //測試 $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é)果:
三個人
二個人
四個人
四個人
五個人
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è)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
上一篇:PHP 實現(xiàn)超簡單的SESSION與COOKIE登錄驗證功能示例
欄 目:PHP編程
本文標題:PHP設(shè)計模式之解釋器(Interpreter)模式入門與應(yīng)用詳解
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/11052.html
您可能感興趣的文章
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂碼
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)語句
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)據(jù)函數(shù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方案
- 04-02php如何用導入數(shù)據(jù) php用來導入其他文件的語句


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導入數(shù)據(jù) php用來導入其他文
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置