PHP Design Patterns -- Interpreter Patterns

 The Interpreter pattern is difficult to understand, but this design pattern is not commonly used.

 It is suitable for programs with relatively simple grammars. Once the grammar complexity is large, it is difficult to maintain in the later stage.
 Define the grammar of a language and build an interpreter to interpret the sentences in the language. Each grammar can be represented as a class. It is only necessary to extend the grammar class and call and interpret through a unified interpreter class.

 For example, there is now an interpreter that can interpret a letter into a complete word. If it needs to be extended, a second grammar class can be created to interpret it into other meanings (such as Chinese).

<?php  
header("Content-Type:text/html;charset=utf-8");  
  
require_once "Interpreter.php";  
  
// Ciphertext interface  
$secretword = new SecretWord();  
  
// add ciphertext  
$secretword->content="A B C D E F";   
  
  
while(!empty($secretword->content))    
{    
    // Two grammars can be used  
    $Grammar = new Grammar1(); // Parse English  
    // $Grammar = new Grammar2(); // Parse Chinese    
  
    // explain  
    echo $Grammar->Translate($secretword)."<br/>";    
}

<?php  
  
  
// Ciphertext interface  
Class SecretWord  
{  
    /**
     * Ciphertext
     * @var string  
     */  
    public $content;  
}  
  
// Interpreter abstract class  
abstract class Interpreter    
{    
  
    /**
     * Explanation method
     * @access public  
     * @param object $secretword ciphertext content
     */  
    public function Translate(SecretWord $secretword)    
    {    
        if(empty($secretword->content))    
        {    
            return false;    
        }    
  
        $key=mb_substr($secretword->content,0,1);    
  
        $secretword->content=mb_substr($secretword->content,1);   
  
        return $this->Excute($key);    
    }   
  
    /**
     * Execute method
     * @access public  
     * @param string $key ciphertext
     */    
    public abstract function Excute($key);    
}    
  
// English grammar class  
Class Grammar1 extends Interpreter  
{  
  
    /**
     * Execute method
     * @access public  
     * @param string $key ciphertext
     */  
    public function Excute($key)  
    {  
        $letter = "";  
        switch($key)  
        {  
            case "A":  
                $letter = "Apple";  
                break;  
            case "B":  
                $letter = "Blue";  
                break;  
            case "C":  
                $letter = "China";  
                break;  
            case "D":  
                $letter = "Double";  
                break;  
            case "E":  
                $letter = "Egg";  
                break;  
            case "F":  
                $letter = "France";  
                break;  
  
        }  
        return $letter;  
    }  
}  
  
// Chinese grammar class  
Class Grammar2 extends Interpreter  
{  
    /**
     * Execute method
     * @access public  
     * @param string $key ciphertext
     */  
    public function Excute($key)  
    {  
        $letter = "";  
        switch($key)  
        {  
            case "A":  
                $letter = "apple";  
                break;  
            case "B":  
                $letter = "Blue";  
                break;  
            case "C":  
                $letter = "China";  
                break;  
            case "D":  
                $letter = "双数";  
                break;  
            case "E":  
                $letter = "egg";  
                break;  
            case "F":  
                $letter = "France";  
                break;  
  
        }  
        return $letter;  
    }  
}  

Output: Grammar1

Apple

Blue

China

Double

Egg

France

Output: Grammar2

apple

blue

china

double

egg

france

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326075292&siteId=291194637