码迷,mamicode.com
首页 > 其他好文 > 详细

解释器模式

时间:2016-08-08 15:47:15      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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


类图:

技术分享


代码:

public abstract class Expression {

	public abstract Object interpreter(Context ctx);
}

public class TerminalExpression extends Expression{

	@Override
	public Object interpreter(Context ctx) {
		// TODO Auto-generated method stub
		return null;
	}

}
public class NonterminalExpression extends Expression{
	
	public NonterminalExpression(Expression ... expressions){
		
	}

	@Override
	public Object interpreter(Context ctx) {
		// TODO Auto-generated method stub
		return null;
	}

}


实例demo代码(加减法)

public abstract class Expression {

	public abstract Object interpreter(HashMap<String,Integer> var);
}

public class VarExpression extends Expression{
	private String key;
	
	public VarExpression(String key){
		this.key = key;
	}

	@Override
	public Object interpreter(HashMap<String, Integer> var) {
		// TODO Auto-generated method stub
		return var.get(key);
	}

}
public abstract class SymbolExpression extends Expression{

	protected Expression left;
	protected Expression right;
	
	public SymbolExpression(Expression left,Expression right){
		this.left = left;
		this.right = right;
	}
}

public class AddExpression extends SymbolExpression{

	public AddExpression(Expression left, Expression right) {
		super(left, right);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int interpreter(HashMap<String, Integer> var) {
		// TODO Auto-generated method stub
		return super.left.interpreter(var) + super.right.interpreter(var);
	}

}
public class SubExpression extends SymbolExpression{

	public SubExpression(Expression left, Expression right) {
		super(left, right);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int interpreter(HashMap<String, Integer> var) {
		// TODO Auto-generated method stub
		return super.left.interpreter(var) - super.right.interpreter(var);
	}

}
public class Calculator {

	private Expression expression;
	
	public Calculator(String expStr){
		Stack<Expression> stack = new Stack<Expression>();
		char[] charArray = expStr.toCharArray();
		Expression left = null;
		Expression right = null;
		for(int i = 0; i<charArray.length;i++){
			switch(charArray[i]){
			case'+':
			    left = stack.pop();
			    right = new VarExpression(String.valueOf(charArray[i]));
			    stack.push(new AddExpression(left,right));
		  	    break;
			case'-':
				left = stack.pop();
				right = new VarExpression(String.valueOf(charArray[i]));
				stack.push(new SubExpression(left,right));
				break;
			default:
				stack.push(new VarExpression(String.valueOf(charArray[i])));
			}
		}
		this.expression = stack.pop();
	}
	
	public int run(HashMap<String,Integer> var){
		return this.expression.interpreter(var);
	}
}

优点:

扩展性,修改语法规则只需修改相应的非终结符表达式


缺点:

解释器模式引起类的膨胀

效率问题



一般不加以使用



解释器模式

标签:

原文地址:http://blog.csdn.net/renpengddxx/article/details/52151746

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!