• 执行及描述任务-------解释器模式


    uml

    代码实现

    <?php
    //interpreter.php
    
    
    abstract class Expression{
        private static $keycount = 0;
        private $key;
    
        //解释
        abstract function interpret(InterpreterContext $context);
    
        function getKey(){
            if(!isset($this->key)){
                self::$keycount++;
                $this->key = self::$keycount;
            }
            return $this->key;
        }
    }
    
    abstract class OperatorExpression extends Expression{
        protected $l_op;
        protected $l_rp;
    
        function __construct(Expression $l_op,Expression $l_rp)
        {
            $this->l_op = $l_op;
            $this->l_rp = $l_rp;
        }
    
        function interpret(InterpreterContext $context)
        {
            $this->l_op->interpret($context);
            $this->l_rp->interpret($context);
            $result_l = $context->lookup($this->l_op);
            $result_r = $context->lookup($this->l_rp);
            $this->doInterpret($context,$result_l,$result_r);
        }
    
        protected abstract function  doInterpret(InterpreterContext $context,$result_l,$result_r);
        
    }
    
    //定义相等表达式
    class EqualsExpression extends OperatorExpression{
        protected function doInterpret(InterpreterContext $context,$result_l,$result_r)
        {
            $context->replace($this,$result_l==$result_r);
        }
    }
    
    //定义布尔或表达式
    class BooleanOrExpression extends OperatorExpression{
        protected function doInterpret(InterpreterContext $context,$result_l,$result_r)
        {
            $context->replace($this,$result_l || $result_r);
        }
    }
    
    //定义布尔与表达式
    class BooleanAndExpression extends OperatorExpression{
        protected function doInterpret(InterpreterContext $context,$result_l,$result_r)
        {
            $context->replace($this,$result_l && $result_r);
        }
    }
    
    //
    class LiteralExpression extends Expression{
        private $value;
    
        function __construct($value){
            $this->value = $value;
        }
    
        function interpret(InterpreterContext $context){
            $context->replace($this,$this->value);
        }
    }
    
    //定义变量(赋值)
    class VariableExpression extends Expression{
        private $name;
        private $val;
    
        function __construct($name,$val=null){
            $this->name = $name;
            $this->val = $val;
        }
    
        function interpret(InterpreterContext $context){
            if(!is_null($this->val)){
                $context->replace($this,$this->val);
                $this->val = null;
            }
        }
    
        function setValue($value){
            $this->val = $value;
        }
    
        function getKey(){
            return    $this->name;
        }
    }
    
    class InterpreterContext{
        private $expressionstore = array();
    
        function replace(Expression $exp,$value){
            $this->expressionstore[$exp->getKey()] = $value;
        }
    
        function lookup(Expression $exp){
            return $this->expressionstore[$exp->getKey()];
        }
    }
    
    
    
    $context = new InterpreterContext();
    $input = new VariableExpression('input');
    
    $statement = new BooleanOrExpression(
        new EqualsExpression($input,new LiteralExpression('four')),
        new EqualsExpression($input,new LiteralExpression('4'))
    );
    
    
    foreach (array('four','4','52') as $val) {
        $input->setValue($val);
        echo "$val<br>";
        $statement->interpret($context);
        if($context->lookup($statement)){
            echo "top marks<br>";
        }else{
            echo "dunce hat on<br>";
        }
    
    }
    ?>

    问题

    创建解释器模式的核心类后,解释器很容易扩展。但是语言变得复杂是,需要创建的类的数量会很快增加。因此解释器模式适用于相对小的语言,如果需要一个全能的编程语言,最好使用第三方工具。

  • 相关阅读:
    (七) rest_framework GenericAPIView/GenericViewSet/ ModelViewSet 解析
    (六) rest_framework 普通分页与加密分页
    (五) rest_framework 序列化与解析器源码实现
    (四) rest_framework 版本控制源码
    【使用 PySpark 分析 CSV 文件】
    安装 HBase1.3.6 on Windows 10 64 bit
    Spark Job 性能调优 (二)
    Spark RDD 分区到底怎么用?
    安装 Spyder python 开发环境 用于 Spark 数据分析 -word count
    安装 Spark on Windows 使用 PySpark
  • 原文地址:https://www.cnblogs.com/rcjtom/p/6068756.html
Copyright © 2020-2023  润新知