PHP设计模式之解释器(Interpreter)模式入门与应用详解
php  /  管理员 发布于 7年前   149
本文实例讲述了PHP设计模式之解释器(Interpreter)模式。分享给大家供大家参考,具体如下: 解释器模式,它是什么呢? 意思就是,给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子,这是最实在的一种说法。 我们还可以理解为它是用于分析一个实体的关键元素,并且针对每个元素提供自己的解释或相应动作。解释器模式非常常用,比如PHP的模板引擎 就是非常常见的一种解释器模。 咱来看一个网上找的最简单的实例: 通过上述实例,大家对于解释器模式肯定有了自己的一个简单理解,我们接下来就看下这个解释器所包含的角色: 完事,咱在网上看的,对于解释器(Interpreter)模式,还有另外一种说法,那就是它包括一个具有复合类分层结构的文法表现,规则是映射到类,跟随在文法后面的表达式可以被转换成一个抽象的语法树,除了复合模式的实例对象图外,没有别的内容。 树是一个抽象的名词,因为实际上大多数时候它是一个表达式的抽象表现,它忽略了可能有一个字符串,也可能有一个数据结构的具体表达式,(例如,在PHP中,“A”和“\x41”是相同抽象字面值的不同具体表现),通过逻辑规则解耦结果,使解释过程大大简化。 但是,对于简单的语法,解释器添加一个规则就象添加一个类那样容易,但解释器没有解决从具体表现形式到抽象语法树的转换,这是由其它服务完成的。 解释器模式旨在为一个简单的抽象表达式(AbstractExpression)方法(解释器操作)实现利用复合分层结构,解释器操作的参数通常统称为上下文,对于给定的一个方法,它们通常被计算值代替,或它们对某些操作可能不存在。 同样,当包含一个解释器时,复合模式的叶子和容器参与者名称会不一样,这些名称反映了它们所扮演的角色:终结符(terminal)或非终结符(nonterminal)表达式。 来看下参与者: ◆客户端(Client):使用解释操作。 我们来看下《设计模式》一书针对这个模式提供的一个扩展示例,是一个网友使用数学表达式替换布尔表达式重新改造了一下,因此这个例子解决了一个数学表达式的展现,它的evaluate( )被分离在一个不同的ConcreteExpression类中,如下: 咱最后再分享一个实例,如下: 结果: 三个人 好啦,本次记录就到这里了。 更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》 希望本文所述对大家PHP程序设计有所帮助。'; 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 = "这是一个模板类,简单的模板类,标题为:";$template = new template;echo $template->run($str);
◆抽象表达式(AbstractExpression):基于一个表达式树抽象。
◆非终结符表达式(NonTerminalExpression):递归地包含其它抽象表达式(AbstractExpression实例)的表达式。
◆终结符表达式(TerminalExpression):不能够进一步简化的表达式。/** * 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...
content; }} //抽象解释器abstract class AbstractInterpreter{ abstract function interpret($content);} //具体解释器,实现抽象解释器的抽象方法class ChineseInterpreter extends AbstractInterpreter{ function interpret($content){ for($i=1;$i
";break; case "2": echo "二个人
";break; case "3": echo "三个人
";break; case "4": echo "四个人
";break; case "5": echo "五个人
";break; case "6": echo "六个人
";break; case "7": echo "七个人
";break; case "8": echo "八个人
";break; case "9": echo "九个人
";break; default:echo "其他"; } } }}class EnglishInterpreter extends AbstractInterpreter{ function interpret($content){ for($i=1;$i
";break; case "2": echo "This is two people
";break; case "3": echo "This is three people
";break; case "4": echo "This is four people
";break; case "5": echo "This is five people
";break; case "6": echo "This is six people
";break; case "7": echo "This is seven people
";break; case "8": echo "This is eight people
";break; case "9": echo "This is nine people
";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您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号