【设计模式】解释器模式

使用频率:★☆☆☆☆

一、什么是解释器模式

定义一些语法规则,然后定义一个解析器去解析该语法;

二、补充说明

该模式极少使用,简单了解下吧~

三、角色(以加减法解析为例)

抽象表达式类

末端表达式类:数字变量

非末端表达式:+-操作

环境角色:存一些变量。。。

客户端

四、例子,JAVA实现

参考维基百科例子

一个解析加减法语法规则的例子

抽象表达式类、以及末端表达式类和非末端表达式类

package com.pichen.dp.behavioralpattern.interpreter;

import java.util.Map;

public interface Expression {
    public int interpret(Map<String, Expression> variables);
}

class Number implements Expression {
    private int number;

    public Number(int number) {
        this.number = number;
    }

    public int interpret(Map<String, Expression> variables) {
        return number;
    }
}

class Plus implements Expression {
    Expression leftOperand;
    Expression rightOperand;

    public Plus(Expression left, Expression right) {
        leftOperand = left;
        rightOperand = right;
    }

    public int interpret(Map<String, Expression> variables) {
        return leftOperand.interpret(variables) + rightOperand.interpret(variables);
    }
}

class Minus implements Expression {
    Expression leftOperand;
    Expression rightOperand;

    public Minus(Expression left, Expression right) {
        leftOperand = left;
        rightOperand = right;
    }

    public int interpret(Map<String, Expression> variables) {
        return leftOperand.interpret(variables) - rightOperand.interpret(variables);
    }
}

class Variable implements Expression {
    private String name;

    public Variable(String name) {
        this.name = name;
    }

    public int interpret(Map<String, Expression> variables) {
        if (null == variables.get(name))
            return 0; // Either return new Number(0).
        return variables.get(name).interpret(variables);
    }
}
package com.pichen.dp.behavioralpattern.interpreter;

import java.util.Map;
import java.util.Stack;

public class Evaluator implements Expression {
    private Expression syntaxTree;

    public Evaluator(String expression) {
        Stack<Expression> expressionStack = new Stack<Expression>();
        for (String token : expression.split(" ")) {
            if (token.equals("+")) {
                Expression subExpression = new Plus(expressionStack.pop(), expressionStack.pop());
                expressionStack.push(subExpression);
            } else if (token.equals("-")) {
                // it's necessary remove first the right operand from the stack
                Expression right = expressionStack.pop();
                // ..and after the left one
                Expression left = expressionStack.pop();
                Expression subExpression = new Minus(left, right);
                expressionStack.push(subExpression);
            } else
                expressionStack.push(new Variable(token));
        }
        syntaxTree = expressionStack.pop();
    }

    public int interpret(Map<String, Expression> context) {
        return syntaxTree.interpret(context);
    }
}

客户端角色,包含环境角色,直接写在main函数里面了

package com.pichen.dp.behavioralpattern.interpreter;
import java.util.Map;
import java.util.HashMap;

public class InterpreterExample {
    public static void main(String[] args) {
        String expression = "w x z - +";
        Evaluator sentence = new Evaluator(expression);
        Map<String,Expression> variables = new HashMap<String,Expression>();
        variables.put("w", new Number(5));
        variables.put("x", new Number(10));
        variables.put("z", new Number(42));
        int result = sentence.interpret(variables);
        System.out.println(result);
    }
}

结果打印:-27

 

posted @ 2016-02-27 11:02  风一样的码农  阅读(1828)  评论(0编辑  收藏  举报