标签:常量 abs null 产生 连接 循环 应用 a* 包含
public abstract class Expression { /** * 以环境为准,本方法解释给定的任何一个表达式 */ public abstract boolean interpret(Context ctx); /** * 检验两个表达式在结构上是否相同 */ public abstract boolean equals(Object obj); /** * 返回表达式的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; } @Override public boolean equals(Object obj) { if(obj != null && obj instanceof Constant){ return this.value == ((Constant)obj).value; } return false; } @Override public int hashCode() { return this.toString().hashCode(); } @Override public boolean interpret(Context ctx) { return value; } @Override public String toString() { return new Boolean(value).toString(); } }
public class Variable extends Expression { private String name; public Variable(String name){ this.name = name; } @Override public boolean equals(Object obj) { if(obj != null && obj instanceof Variable) { return this.name.equals( ((Variable)obj).name); } return false; } @Override public int hashCode() { return this.toString().hashCode(); } @Override public String toString() { return name; } @Override public boolean interpret(Context ctx) { return ctx.lookup(this); } }
public class And extends Expression { private Expression left,right; public And(Expression left , Expression right){ this.left = left; this.right = right; } @Override public boolean equals(Object obj) { if(obj != null && obj instanceof And) { return left.equals(((And)obj).left) && right.equals(((And)obj).right); } return false; } @Override public int hashCode() { return this.toString().hashCode(); } @Override public boolean interpret(Context ctx) { return left.interpret(ctx) && right.interpret(ctx); } @Override 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; } @Override public boolean equals(Object obj) { if(obj != null && obj instanceof Or) { return this.left.equals(((Or)obj).left) && this.right.equals(((Or)obj).right); } return false; } @Override public int hashCode() { return this.toString().hashCode(); } @Override public boolean interpret(Context ctx) { return left.interpret(ctx) || right.interpret(ctx); } @Override public String toString() { return "(" + left.toString() + " OR " + right.toString() + ")"; } }
public class Not extends Expression { private Expression exp; public Not(Expression exp){ this.exp = exp; } @Override public boolean equals(Object obj) { if(obj != null && obj instanceof Not) { return exp.equals( ((Not)obj).exp); } return false; } @Override public int hashCode() { return this.toString().hashCode(); } @Override public boolean interpret(Context ctx) { return !exp.interpret(ctx); } @Override public String toString() { return "(Not " + exp.toString() + ")"; } }
public class Context { private Map<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 = map.get(var); if(value == null){ throw new IllegalArgumentException(); } return value.booleanValue(); } }
public class Client { public static void main(String[] args) { Context 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); Expression 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)); } }
设计模式(15)--Interpreter(解释器模式)--行为型
标签:常量 abs null 产生 连接 循环 应用 a* 包含
原文地址:http://www.cnblogs.com/yysbolg/p/7469634.html