论坛首页 Java企业应用论坛

解释器(Interpreter)模式【行为模式第八篇】

浏览 2970 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (4)
作者 正文
   发表时间:2009-10-23   最后修改:2010-10-20
解释器(Interpreter)模式:
解释器模式是类的行为模式。给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器。客户端可以使用这个解释器
来解释这个语言中的句子。

一、解释器模式所涉及的角色
1、抽象表达式角色:声明一个所有的具体表达式角色都需要实现的抽象接口。这个接口主要是一个interpret()方法,称做解释操作。
2、终结符表达式角色:这是一个具体角色。
(1)实现了抽象表达式角色所要求的接口,主要是一个interpret()方法;
(2)文法中的每一个终结符都有一个具体终结表达式与之相对应。
3、非终结符表达式角色:这是一个具体角色。
(1)文法中的每一条规则 R=R1R2.....Rn 都需要一个具体的非终结符表达式类;
(2)对每一个 R1R2.....Rn 中的符号都持有一个静态类型为Expression的实例变量。
(3)实现解释操作,即 interpret()方法。解释操作以递归方式调用上面所提到的代表 R1R2.....Rn  中的各个符号的实
     例变量
4、客户端角色:代表模式的客户端它有以下功能
(1)建造一个抽象语法树(AST或者Abstract Syntax Tree)
(2)调用解释操作interpret()。
5、环境角色:(在一般情况下,模式还需要一个环境角色)提供解释器之外的一些全局信息,比如变量的真实量值等。

(抽象语法树的每一个节点都代表一个语句,而在每一个节点上都可以执行解释方法。这个解释方法的执行就代表这个语句被解释。
  由于每一个语句都代表对一个问题实例的解答。)

 //抽象角色Expression
		  /*
		  	这个抽象类代表终结类和非终结类的抽象化
		  	其中终结类和非终结类来自下面的文法
		  	Expression ::=
		  			Expression AND Expression
		  			| Expression OR Expression
		  			| NOT Expression
		  			| Variable
		  			| Constant
		  	Variable ::= ....//可以打印出的非空白字符串
		  	Constant ::= "true" | "false"
		  */
		  
		  public abstract class Expression{
		  	//以环境类为准,本方法解释给定的任何一个表达式
		  	public abstract boolean interpret(Context ctx);
		  	
		  	//检验两个表达式在结构上是否相同
		  	public abstract boolean equals(Object o);
		  	
		  	//返回表达式的hash code
		  	public abstract int hashCode();
		  	
		  	//将表达式转换成字符串
		  	public abstract String toString();
		  }
		  
		  public class Constant extends Expression{
		  	private boolean value;
		  	
		  	public Constant(boolean value){
		  		this.value = value;
		  	}
		  	//解释操作
		  	public boolean interpret(Context ctx){
		  		return value;
		  	}
		  	
		  	//检验两个表达式在结构上是否相同
		  	public boolean equals(Object o){
		  		if(o != null && o instanceof Constant){
		  			return this.value == ((Constant)o).value;
		  		}
		  		return false;
		  	}
		  	
		  	//返回表达式的hash code
		  	public int hashCode(){
		  		return (this.toString()).hashCode();
		  	}
		  	
		  	//将表达式转换成字符串
		  	public String toString(){
		  		return new Boolean(value).toString();
		  	}
		  }
		  
		  public class Variable extends Expression{
		  	private String name;
		  	public Variable(String name){
		  		this.name = name;
		  	}
		  	
		  	public boolean interpret(Context ctx){
		  		return ctx.lookup(this);
		  	}
		  	
		  	public boolean equals(Object o){
		  		if(o != null && o instanceof Variable){
		  			return this.name.equals(((Variable)o).name);
		  		}
		  		return false;
		  	}
		  	
		  	public int hashCode(){
		  		return (this.toString()).hashCode();
		  	}
		  	
		  	public String toString(){
		  		return name;
		  	}
		  }
		  
		  public class And extends Expression{
		  	private Expression left,right;
		  	
		  	public And(Expression left,Expression right){
		  		this.left = left;
		  		this.right = right;
		  	}
		  	
		  	public boolean interpret(Context ctx){
		  		return left.interpret(ctx) &&
		  			right.interpret(ctx);
		  	}
		  	
		  	public boolean equlas(Object o){
		  		if(o != null && o instanceof And){
		  			return this.left.equals(((And)o).left) &&
		  				this.right.equals(((And)o).right);
		  		}
		  		return false;
		  	}
		  	
		  	public int hashCode(){
		  		return (this.toString()).hashCode();
		  	}
		  	
		  	public String toString(){
		  		return "(" + left.toString() + "AND" + right.toString() + ")";
		  	}
		  }
		  
		  public class Or extends Expression{
		  	private Expression left , right;
		  	
		  	public Or(Expression left,Expression right){
		  		this.left = left;
		  		this.right = right;
		  	}
		  	
		  	public boolean interpret(Context ctx){
		  		return left.interpret(ctx) || right.interpret(ctx);
		  	}
		  	
		  	public boolean equals(Object o){
		  		if(o != null && o instanceof Or){
		  			return this.left.equals(((And)o).left) &&
		  				this.right.equals(((And)o).right);
		  		}
		  		return false;
		  	}
		  	
		  	public int hashCode(){
		  		return (this.toString()).hashCode();
		  	}
		  	
		  	public String toString(){
		  		return "(" + left.toString() + "OR" + right.toString() + ")";
		  	}
		  }
		  
		   public class Not extends Expression{
		  	private Expression exp;
		  	
		  	public Not(Expression exp){
		  		this.exp = exp;
		  	}
		  	
		  	public boolean interpret(Context ctx){
		  		return !exp.interpret(ctx);
		  	}
		  	
		  	public boolean equals(Object o){
		  		if(o != null && o instanceof Not){
		  			return this.exp.equals(((Not)o).exp);
		  		}
		  		return false;
		  	}
		  	
		  	public int hashCode(){
		  		return (this.toString()).hashCode();
		  	}
		  	
		  	public String toString(){
		  		return "(NOT" + exp.toString() + ")";
		  	}
		  }
		  
		  import java.util.HashMap;
		  
		  public class Context{
		  	private HashMap map = new HashMap();
		  	public void assign(Variable var,boolean value){
		  		map.put(var,new Boolean(value));
		  	}
		  	
		  	public boolean lookup(Variable var) throws IllegalArgumentException{
		  		Boolean value = (Boolean)map.get(var);
		  		if(value == null){
		  			throw new IllegalArgumentException();
		  		}
		  		return value.booleanValue();
		  	}
		  }
		  
		  //客户端
		  public class Client{
		  	private static Context ctx;
		  	private static Expression exp;
		  	
		  	public static void main(String args[]){
		  		ctx = new Context();
		  		Variable x = new Variable("x");
		  		Variable y = new Variable("y");
		  		
		  		Constant c = new Constant(true);
		  		ctx.assign(x,false);
		  		ctx.assign(y,true);
		  		
		  		exp = new Or(new And(c,x),new And(y,new Not(x)));
		  		System.out.println("x = " + x.interpret(ctx));
		  		System.out.println("y = " + y.interpret(ctx));
		  		System.out.println(exp.toString() + "=" + exp.interpret(ctx));
		  	}
		  }

二、解释器模式适用于以下的情况:
(1)系统有一个简单的语言可供解释
(2)一些重复发生的问题可以用这种简单的语言表达
(3)效率不是主要的考虑。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics