设计模式学习笔记二十:解释器模式
动机
解释器(INTERPRETER)模式 ,属于类行为型模式。给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。(摘自《GoF设计模式》)
个人见解
自己创造编程语言或者DSL的时候能用到,除此之外,个人认为基本没有什么场景。存在的目的就是为了个团圆,况且编译原理已经还给老师了。
再有就是这个跟状态机的调用流程很像。
UML结构图:
代码实现
注:代码内容实现来自http://www.cnblogs.com/java-my-life/archive/2012/06/19/2552617.html
1.Expression,即AbstractExpression
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();
}
2.Constant,即TerminalExpression
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();
}
}
3.Variable
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);
}
}
4.And Or Not
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() + ")";
}
}
5.Context
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();
}
}
6.TestDriver
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));
}
}