搜索
首页后端开发php教程php 设计模式之解释器模式的深入解析详解

php 设计模式之解释器模式的深入解析详解

Jul 09, 2017 am 09:34 AM
php解释器设计模式

本篇文章是对PHP设计模式中的解释器模式进行了详细的分析介绍,需要的朋友参考下

解释器(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 设计模式之解释器模式的深入解析详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP中的依赖注入:避免常见的陷阱PHP中的依赖注入:避免常见的陷阱May 16, 2025 am 12:17 AM

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

如何加快PHP网站:性能调整如何加快PHP网站:性能调整May 16, 2025 am 12:12 AM

到Improveyourphpwebsite的实力,UsEthestertate:1)emplastOpCodeCachingWithOpcachetCachetOspeedUpScriptInterpretation.2)优化的atabasequesquesquesquelies berselectingOnlynlynnellynnessaryfields.3)usecachingsystemssslikeremememememcachedisemcachedtoredtoredtoredsatabaseloadch.4)

通过PHP发送大规模电子邮件:有可能吗?通过PHP发送大规模电子邮件:有可能吗?May 16, 2025 am 12:10 AM

是的,itispossibletosendMassemailswithp.1)uselibrarieslikeLikePhpMailerorSwiftMailerForeffitedEmailSending.2)enasledeLaysBetemailStoavoidSpamflagssspamflags.3)sylectynamicContentToimpovereveragement.4)

PHP中依赖注入的目的是什么?PHP中依赖注入的目的是什么?May 16, 2025 am 12:10 AM

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

如何使用PHP发送电子邮件?如何使用PHP发送电子邮件?May 16, 2025 am 12:03 AM

使用PHP发送电子邮件的最佳方法包括:1.使用PHP的mail()函数进行基本发送;2.使用PHPMailer库发送更复杂的HTML邮件;3.使用SendGrid等事务性邮件服务提高可靠性和分析能力。通过这些方法,可以确保邮件不仅到达收件箱,还能吸引收件人。

如何计算PHP多维数组的元素总数?如何计算PHP多维数组的元素总数?May 15, 2025 pm 09:00 PM

计算PHP多维数组的元素总数可以使用递归或迭代方法。1.递归方法通过遍历数组并递归处理嵌套数组来计数。2.迭代方法使用栈来模拟递归,避免深度问题。3.array_walk_recursive函数也能实现,但需手动计数。

PHP中do-while循环有什么特点?PHP中do-while循环有什么特点?May 15, 2025 pm 08:57 PM

在PHP中,do-while循环的特点是保证循环体至少执行一次,然后再根据条件决定是否继续循环。1)它在条件检查之前执行循环体,适合需要确保操作至少执行一次的场景,如用户输入验证和菜单系统。2)然而,do-while循环的语法可能导致新手困惑,且可能增加不必要的性能开销。

PHP中如何哈希字符串?PHP中如何哈希字符串?May 15, 2025 pm 08:54 PM

在PHP中高效地哈希字符串可以使用以下方法:1.使用md5函数进行快速哈希,但不适合密码存储。2.使用sha256函数提高安全性。3.使用password_hash函数处理密码,提供最高安全性和便捷性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)