您的位置:首页 > 其它

设计模式-结构型模式-解释器

2011-05-04 15:47 417 查看
行为模式涉及到算法和对象间职责的分配。
interpreter:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器用来解释语言中的句子。

package behaviour.interpreter;

import behaviour.interpreter.mypackage.BooleanExp;

/**
* A NonterminalExpression
*/
public class AndExp implements BooleanExp {
private BooleanExp operand1;
private BooleanExp operand2;

public AndExp(BooleanExp oper1, BooleanExp oper2) {
operand1 = oper1;
operand2 = oper2;
}

public boolean Evaluate(Context c) {
return operand1.Evaluate(c) &&
operand2.Evaluate(c);
}

public BooleanExp Copy() {
return new AndExp(operand1.Copy(), operand2.Copy());
}

public BooleanExp Replace(String var, BooleanExp exp) {
return new AndExp(
operand1.Replace(var, exp),
operand2.Replace(var, exp)
);
}
}

package behaviour.interpreter;
/**
* A Context to record variable value
*/
import java.util.*;

public class Context {
private Hashtable context = new Hashtable();
public void Assign(String name, boolean val) {
context.put(name, new Boolean(val));
}
public boolean LookUp(String name) {
return ((Boolean)context.get(name)).booleanValue();
}
public Context() {
}
}

package behaviour.interpreter;

import behaviour.interpreter.mypackage.BooleanExp;

/**
* A NonterminalExpression
*/
public class NotExp implements BooleanExp {
private BooleanExp opernot1;
//private BooleanExp operor2;

public NotExp(BooleanExp oper1) {
opernot1 = oper1;
}

public boolean Evaluate(Context c) {
return !(opernot1.Evaluate(c));
}

public BooleanExp Copy() {
return new NotExp(opernot1.Copy());
}

public BooleanExp Replace(String var, BooleanExp exp) {
return new NotExp(opernot1.Replace(var, exp));
}
}

package behaviour.interpreter;

import behaviour.interpreter.mypackage.BooleanExp;

/**
* A NonterminalExpression
*/
public class OrExp implements BooleanExp {
private BooleanExp operor1;
private BooleanExp operor2;

public OrExp(BooleanExp oper1, BooleanExp oper2) {
operor1 = oper1;
operor2 = oper2;
}

public boolean Evaluate(Context c) {
return operor1.Evaluate(c) ||
operor2.Evaluate(c);
}

public BooleanExp Copy() {
return new OrExp(operor1.Copy(), operor2.Copy());
}

public BooleanExp Replace(String var, BooleanExp exp) {
return new OrExp(
operor1.Replace(var, exp),
operor2.Replace(var, exp)
);
}
}

package behaviour.interpreter;
/**
*
*/
import java.io.*;
import java.util.*;

import behaviour.interpreter.mypackage.BooleanExp;

public class Test {
public static void main(String[] args) {
// Test :
// (true and x) and (y and (not x))
Context context = new Context();

VariableExp x = new VariableExp("X");
VariableExp y = new VariableExp("Y");
VariableExp bTure = new VariableExp("true");
VariableExp bFalse = new VariableExp("false");

context.Assign("true", true);
context.Assign("false", false);
context.Assign("X", false);
context.Assign("Y", true);

BooleanExp expression = new AndExp(
new AndExp(bTure, x),
new AndExp(y, new NotExp(x))
);
boolean result = expression.Evaluate(context);
System.out.println("The result is:" + result);
}
}

package behaviour.interpreter;
/**
* A variable expression implements BooleanExp
* A terminal expression
*/
import java.util.*;

import behaviour.interpreter.mypackage.BooleanExp;

public class VariableExp implements BooleanExp {
private String name;

public VariableExp(String _name) {
name = _name;
}

public boolean Evaluate(Context c) {
return c.LookUp(name);
}

public BooleanExp Copy() {
return new VariableExp(name);
}

public BooleanExp Replace(String var, BooleanExp exp) {
if(var.equals(name)) {
return exp.Copy();
} else {
return new VariableExp(name);
}
}

}

package behaviour.interpreter.mypackage;

import behaviour.interpreter.Context;

/**
* The interface of our BooleanExp Interpreter
* BooleanExp definition is:
* BooleanExp ::= VariableExp | Constant | OrExp | AndExp
* | NotExp | '(' BooleanExp ')'
* AndExp ::= BooleanExp 'and' BooleanExp
* OrExp ::= BooleanExp 'or' BooleanExp
* NotExp ::= BooleanExp 'not' BooleanExp
* Constant ::= 'true' | 'false'
* VariableExp ::= 'A' | 'B' | ... | 'Z'
*/

public interface BooleanExp {
public abstract boolean Evaluate(Context c);
public abstract BooleanExp Replace(String var, BooleanExp exp);
public abstract BooleanExp Copy();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: