1 public class Client { 2 private static Context ctx; 3 private static Expression exp; 4 public static void main(String[] args) { 5 ctx = new Context(); 6 Variable x = new Variable("x"); 7 Variable y = new Variable("y"); 8 Constant c = new Constant(true); 9 ctx.assign(x, false); 10 ctx.assign(y, true); 11 exp = new Or(new And(c,x),new And(y,new Not(x))); 12 System.out.println("x = "+x.interpret(ctx)); 13 System.out.println("y = "+y.interpret(ctx)); 14 System.out.println(exp.toString()+"=" +exp.interpret(ctx)); 15 } 16 } 17 18 //抽象角色Expression 19 /* 20 * 這個抽象類表明終結類和非終結類的抽象化 21 * 其中終結類和非終結類來自下面的文法 22 * Expression ::= 23 * Expression AND Expression 24 * |Expression OR Expression 25 * |NOT Expression 26 * |Variable 27 * |Constant 28 * Variable ::=...//能夠打印出的非空白字符串 29 * Contant ::="true"|"false" 30 */ 31 abstract class Expression{ 32 //以環境類爲準,本方法解釋給定的任何一個表達式 33 public abstract boolean interpret(Context ctx); 34 //檢驗兩個表達式在結構上是否相同 35 public abstract boolean equals(Object o); 36 //返回表達式的hashcode 37 public abstract int hashCode(); 38 //將表達式轉換成字符串 39 public abstract String toString(); 40 } 41 42 //Constant類,經過構造子傳入一個布爾常量,獲得一個包裝了這個布爾常量的對象,new Constant(true) 43 class Constant extends Expression{ 44 45 private boolean value; 46 //構造子 47 public Constant(boolean value){ 48 this.value = value; 49 } 50 //解釋操做 51 @Override 52 public boolean interpret(Context ctx) { 53 return value; 54 } 55 //檢驗兩個表達式是否相同 56 @Override 57 public boolean equals(Object o) { 58 if(o!=null&&o instanceof Constant){ 59 return this.value = ((Constant)o).value; 60 } 61 return false; 62 } 63 64 @Override 65 public int hashCode() { 66 return (this.toString()).hashCode(); 67 } 68 69 @Override 70 public String toString() { 71 return new Boolean(value).toString(); 72 } 73 74 } 75 //Variable類 使用時將變量名傳入構造子中,如new Variable("x")就聲明瞭一個名字爲x的變量 76 class Variable extends Expression{ 77 private String name; 78 79 public Variable(String name){ 80 this.name = name; 81 } 82 //解釋操做 83 @Override 84 public boolean interpret(Context ctx) { 85 try { 86 return ctx.lookup(this); 87 } catch (Exception e) { 88 e.printStackTrace(); 89 } 90 return false; 91 } 92 93 @Override 94 public boolean equals(Object o) { 95 if(o!=null&&o instanceof Variable){ 96 return this.name.equals(((Variable)o).name); 97 } 98 return false; 99 } 100 101 //返回hashcode 102 @Override 103 public int hashCode() { 104 return (this.toString()).hashCode(); 105 } 106 107 //將表達式轉換成字符串 108 @Override 109 public String toString() { 110 return name; 111 } 112 } 113 //And類 new And(x,y)表明x And y 114 class And extends Expression{ 115 116 private Expression left,right; 117 public And(Expression left,Expression right){ 118 this.left = left; 119 this.right = right; 120 } 121 //解釋操做 122 @Override 123 public boolean interpret(Context ctx) { 124 return left.interpret(ctx)&&right.interpret(ctx); 125 } 126 127 @Override 128 public boolean equals(Object o) { 129 if(o!=null&&o instanceof And){ 130 return this.left.equals(((And)o).left)&&this.right.equals(((And)o).right); 131 } 132 return false; 133 } 134 135 @Override 136 public int hashCode() { 137 return this.toString().hashCode(); 138 } 139 //將表達式轉換成字符串 140 @Override 141 public String toString() { 142 return "("+left.toString()+"AND"+right.toString()+")"; 143 } 144 145 } 146 //Or類 147 class Or extends Expression{ 148 149 private Expression left,right; 150 public Or(Expression left,Expression right){ 151 this.left = left; 152 this.right = right; 153 } 154 //解釋操做 155 @Override 156 public boolean interpret(Context ctx) { 157 return left.interpret(ctx)&&right.interpret(ctx); 158 } 159 160 @Override 161 public boolean equals(Object o) { 162 if(o!=null&&o instanceof And){ 163 return this.left.equals(((Or)o).left)&&this.right.equals(((Or)o).right); 164 } 165 return false; 166 } 167 168 @Override 169 public int hashCode() { 170 return this.toString().hashCode(); 171 } 172 //將表達式轉換成字符串 173 @Override 174 public String toString() { 175 return "("+left.toString()+"OR"+right.toString()+")"; 176 } 177 } 178 //Not類 179 class Not extends Expression{ 180 private Expression exp; 181 public Not(Expression exp){ 182 this.exp = exp; 183 } 184 @Override 185 public boolean interpret(Context ctx) { 186 return !exp.interpret(ctx); 187 } 188 @Override 189 public boolean equals(Object o) { 190 if(o!=null&& o instanceof Not){ 191 return this.exp.equals(((Not)o).exp); 192 } 193 return false; 194 } 195 @Override 196 public int hashCode() { 197 return this.toString().hashCode(); 198 } 199 @Override 200 public String toString() { 201 return "(Not" + exp.toString() +")"; 202 } 203 } 204 205 //Context 206 class Context{ 207 private HashMap map = new HashMap(); 208 public void assign(Variable var,boolean value){ 209 map.put(var, new Boolean(value)); 210 } 211 public boolean lookup(Variable var)throws Exception{ 212 Boolean value = (Boolean)map.get(var); 213 if(value ==null){ 214 throw new Exception(); 215 } 216 return value.booleanValue(); 217 } 218 }