PHP设计模式--解释器模式
声明:本系列博客参考资料《大话设计模式》,作者程杰。
解释器模式:Given a language, define arepresentation for its grammar along with an interpreter that uses therepresentation to interpret sentences in the language。给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。
类图:
角色:
环境角色(PlayContent):定义解释规则的全局信息。
抽象解释器(Empress):定义了部分解释具体实现,封装了一些由具体解释器实现的接口。
具体解释器(MusicNote):实现抽象解释器的接口,进行具体的解释执行。
核心代码:(注意:需要开启extension=php_mbstring.dll扩展)
<?php /** * Created by PhpStorm. * User: Jiang * Date: 2015/5/31 * Time: 15:51 *//**环境角色 * Class PlayContent */class PlayContent{ public $content;}/**抽象解析器 * Class IExpress */abstract class IExpress{ //-----------------解释器---------------- public function Translate(PlayContent $play_content) { if(empty($play_content->content)) { return false; } $key=mb_substr($play_content->content,0,1); $play_content->content=mb_substr($play_content->content,2); $val=mb_substr($play_content->content,0,mb_strpos($play_content->content,' ')); $play_content->content=mb_substr($play_content->content,mb_strpos($play_content->content,' ')+1); return $this->Excute($key,$val); } public abstract function Excute($key,$val);}//------------------------具体解析器-------------/**音符 * Class MusicNote */class MusicNote extends IExpress{ public function Excute($key,$val) { $note=""; switch($key) { case "C": $note = "1"; break; case "D": $note = "2"; break; case "E": $note = "3"; break; case "F": $note = "4"; break; case "G": $note = "5"; break; case "A": $note = "6"; break; case "B": $note = "7"; break; } return $note; }}/**音阶 * Class MusicScale */class MusicScale extends IExpress{ public function Excute($key,$val) { $scale=""; switch($val) { case "1": $scale="低音"; break; case "2": $scale="中音"; break; case "3": $scale="高音"; break; } return $scale; }}
调用客户端代码:
header("Content-Type:text/html;charset=utf-8");//-------------------------解释器模式-------------------------------require_once "./Interpreter/Interpreter.php";$play_content=new PlayContent();$play_content->content="O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 ";$interpreter=null;try{ while(!empty($play_content->content)) { $str = mb_substr($play_content->content,0,1); switch($str) { case "O": $interpreter = new MusicScale(); break; case "C": case "D": case "E": case "F": case "G": case "A": case "B": case "P": $interpreter = new MusicNote(); break; } echo $interpreter->Translate($play_content).'::'; }}catch(Exception $e){ echo $e->getMessage();}
优点:
解释器是一个简单语法分析工具,它最显著的优点就是扩展性,修改语法规则只要修改相应的非终结符表达式就可以了,若扩展语法,则只要增加非终结符类就可以了。
缺点:
1.解释器模式会引起类膨胀
2.每个语法都要产生一个非终结符表达式,语法规则比较复杂时,就可能产生大量的类文件,为维护带来了非常多的麻烦。
3.解释器模式采用递归调用方法
每个非终结符表达式只关心与自己有关的表达式,每个表达式需要知道最终的结果,必须一层一层地剥茧,无论是面向过程的语言还是面向对象的语言,递归都是在必要条件下使用的,它导致调试非常复杂。想想看,如果要排查一个语法错误,我们是不是要一个一个断点的调试下去,直到最小的语法单元。
效率问题
解释器模式由于使用了大量的循环和递归,效率是个不容忽视的问题,特别是用于解析复杂、冗长的语法时,效率是难以忍受的。
适用场景:
1、重复发生的问题可以使用解释器模式
例如,多个应用服务器,每天产生大量的日志,需要对日志文件进行分析处理,由于各个服务器的日志格式不同,但是数据要素是相同的,按照解释器的说法就是终结符表达式都是相同的,但是非终结符表达式就需要制定了。在这种情况下,可以通过程序来一劳永逸地解决该问题。
2、一个简单语法需要解释的场景
为什么是简单?文法规则越多,复杂度越高,而且类间还要进行递归调用,不是一般地复杂。想想看,多个类之间的调用你需要什么样的耐心和信心去排查问题。因此,解释器模式一般用来解析比较标准的字符集,例如SQL语法分析,不过该部分逐渐被专用工具所取代。
欢迎关注我的视频课程,地址如下,谢谢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.
