本文實例講述了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...
咱最后再分享一個實例,如下:
?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;$icount($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;$icount($content);$i++){
switch($content[$i]){
case '0': echo "This is nobodybr>";break;
case "1": echo "This is one peoplebr>";break;
case "2": echo "This is two peoplebr>";break;
case "3": echo "This is three peoplebr>";break;
case "4": echo "This is four peoplebr>";break;
case "5": echo "This is five peoplebr>";break;
case "6": echo "This is six peoplebr>";break;
case "7": echo "This is seven peoplebr>";break;
case "8": echo "This is eight peoplebr>";break;
case "9": echo "This is nine peoplebr>";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程序設計有所幫助。
您可能感興趣的文章:- php設計模式 Builder(建造者模式)
- 學習php設計模式 php實現建造者模式
- PHP設計模式之建造者模式定義與用法簡單示例
- PHP設計模式之適配器模式(Adapter)原理與用法詳解
- PHP設計模式之策略模式(Strategy)入門與應用案例詳解
- PHP設計模式之單例模式入門與應用詳解
- PHP設計模式之觀察者模式入門與應用案例詳解
- PHP設計模式之中介者模式(Mediator Pattern)入門與應用案例詳解
- PHP設計模式之迭代器(Iterator)模式入門與應用詳解
- PHP設計模式之觀察者模式(Observer)詳細介紹和代碼實例
- PHP經典面試題之設計模式(經常遇到)
- PHP設計模式之建造者模式(Builder)原理與用法案例詳解