解释器方式(Interpreter)-java与模式(例子)

解释器模式(Interpreter)---java与模式(例子)

 

一,Interpreter使用面不是很广,描述了一个语言解释器是怎么构成的,在实际应用中,我们可能很少去构造一个语言的解释器.

没在平常使用中发现例子,故以java与模式一书中的例子为例。。。

这个例子是针对 与,或,非提供的一套解析器

 

提供一个实现的环境

 

import java.util.HashMap;
import java.util.Map;

/**
 *提供一个boolean环境,用于作判断
 * @author wu_quanyin(09817) 
 * @version 1.0
 * @date Jul 9, 2010 9:39:24 PM
 */
public class Context {

	private Map map=new HashMap();
	
	public void assign(Variable var,boolean value){
		map.put(var, new Boolean(value));
	}
	
	public boolean lookup(Variable var)throws IllegalAccessException{
		Boolean value=(Boolean)map.get(var);
		if(value==null){
			throw new IllegalAccessException();
		}
		return value.booleanValue();
	}
}

 解析器的接口

 

/**
 *进行and or !操作的抽象
 * 
 * @author wu_quanyin(09817) 
 * @version 1.0
 * @date Jul 9, 2010 9:36:54 PM
 */
public interface Expression {

      //用于判断操作
	public boolean interpret(Context ctx);
	
	public boolean equals(Object o);
	
	public int hashCode();
	
	public String toString();
}

 //And的操作

 

/**
 * i与操作
 * @author wu_quanyin(09817) 
 * @version 1.0
 * @date Jul 9, 2010 9:47:12 PM
 */
public class And implements Expression {

	private Expression left,right;
	
	public And(Expression left,Expression right){
		this.left=left;
		this.right=right;
	}
	
	public boolean interpret(Context ctx) {
		// TODO Auto-generated method stub
		return left.interpret(ctx)&&right.interpret(ctx);
	}
	
	public boolean equals(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()+")";
	}

}

 //or操作

 

/**
 * 或操作
 * 
 * @author wu_quanyin(09817)
 * @version 1.0
 * @date Jul 9, 2010 9:53:04 PM
 */
public class Or implements Expression {

	private Expression left, right;

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

	public boolean interpret(Context ctx) {
		// TODO Auto-generated method stub
		return left.interpret(ctx) || right.interpret(ctx);
	}

	public boolean equals(Object o) {
		if (o != null && o instanceof Or) {
			return this.left.equals(((Or) o).left)
					|| this.right.equals(((Or) o).right);
		}
		return false;
	}

	public int hashCode() {
		return (this.toString()).hashCode();
	}

	public String toString() {
		return "(" + left.toString() + "OR" + right.toString() + ")";
	}

}

 //Not操作

 

/**
 * 非操作
 * @author wu_quanyin(09817) 
 * @version 1.0
 * @date Jul 9, 2010 9:40:35 PM
 */
public class Constant implements 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;
	}
	
	public int hashCode(){
		return(this.toString()).hashCode();
	}
	
	public String toString(){
		return new Boolean(value).toString();
	}
}

 //-----------------------为其提供操作的类型Variable Constant

 

Variable

 

/**
 * 使用相应的名称到context中进行查找
 * @author wu_quanyin(09817) 
 * @version 1.0
 * @date Jul 9, 2010 9:44:36 PM
 */
public class Variable implements Expression {

private String name;
	
	public Variable(String name){
		this.name=name;
	}
	
	public boolean interpret(Context ctx) {
		try {
			return ctx.lookup(this);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean equals(Object o){
		if(o!=null&&o instanceof Constant){
			return this.name.equals(((Variable)o).name);
		}
		return false;
	}
	
	public int hashCode(){
		return(this.toString()).hashCode();
	}
	
	public String toString(){
		return name;
	}

}

 //Constant

 

/**
 * 返回固定类型
 * @author wu_quanyin(09817) 
 * @version 1.0
 * @date Jul 9, 2010 9:40:35 PM
 */
public class Constant implements 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;
	}
	
	public int hashCode(){
		return(this.toString()).hashCode();
	}
	
	public String toString(){
		return new Boolean(value).toString();
	}
}
 

 

 

//------------------------------------test

 

可在此对and or not进行任意的操作

/**
 * 
 * @author wu_quanyin(09817) 
 * @version 1.0
 * @date Jul 9, 2010 10:01:31 PM
 */
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(exp.interpret(ctx));
	}
}
/**
--false
*/