设为首页 加入收藏

TOP

解释器模式和php实现(一)
2019-09-03 01:29:15 】 浏览:36
Tags:解释 模式 php 实现

解释器模式:
  给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。

角色:
  环境角色:定义解释规则的全局信息。
  抽象解释器::定义了部分解释具体实现,封装了一些由具体解释器实现的接口。
  具体解释器(MusicNote):实现抽象解释器的接口,进行具体的解释执行。

UML图:

    clip_image013

代码实现:

<?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;$i<count($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;$i<count($content);$i++){
             switch($content[$i]){
                case '0': echo "This is nobody<br>";break;
                case "1": echo "This is one people<br>";break;
                case "2": echo "This is two people<br>";break;
                case "3": echo "This is three people<br>";break;
                case "4": echo "This is four people<br>";break;
                case "5": echo "This is five people<br>";break;
                case "6": echo "This is six people<br>";break;
                case "7": echo "This is seven people<br>";break;
                case "8": echo "This is eight people<br>";break;
                case "9": echo "This is nine people<br>";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
?>

 

 

适用性:
  当有一个语言需要解释执行, 并且你可将该语言中的句子表示为一个抽象语法树时,可使用解释器模式

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Composite(组合)--对象结构型模.. 下一篇Java设计模式(十二) 策略模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目