Interpreter,解释器模式:给定一个语言,定义他的文法的一个表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
package com.qinsoft.design; import java.util.HashMap; abstract class Expression { //以环境类为准,本方法解释给定的任何一个表达式 public abstract boolean interpret(Content ctx); //检验两个表达式在结构上是否相同 public abstract boolean equals(Object o); //返回表达式的hash code public abstract int hashCode(); //将表达式转换成字符串 public abstract String toString(); } class Constant extends Expression { private boolean value; public Constant(boolean value) { this.value = value; } //解释操作 public boolean interpret(Content 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(); } } class Variable extends Expression { private String name; public Variable(String name) { this.name = name; } public boolean interpret(Content 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; } } class And extends Expression { public Expression left, right; public And(Expression left, Expression right) { this.left = left; this.right = right; } public boolean interpret(Content ctx) { 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() + ")"; } } class Or extends Expression { private Expression left, right; public Or(Expression left, Expression right) { this.left = left; this.right = right; } public boolean interpret(Content 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() + ")"; } } class Not extends Expression { private Expression exp; public Not(Expression exp) { this.exp = exp; } public boolean interpret(Content 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() + ")"; } } class Content { private HashMap<Variable, Boolean> map = new HashMap<Variable, Boolean>(); 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 Interpreter { private static Content ctx; private static Expression exp; public static void main(String args[]) { ctx = new Content(); 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)); } }