设计模式-结构型模式-解释器
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();
}
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();
}
相关文章推荐
- 翻转设计模式系列——第三部分--设计模式(2)--结构型模式大比拼
- 设计模式之结构型模式―― 2.7 代理模式
- 【23种设计模式】结构型模式 > 桥接模式
- 设计模式之七大结构型模式(附实例和详解)
- 结构型对象设计模式
- [设计模式笔记]二. 结构型模式--8.Bridge模式(桥模式)(一)
- 设计模式之十四:Decorator(装饰)—对象结构型模式
- java设计模式--代理模式(结构型)
- 设计模式笔记 12. Proxy 代理模式(结构型模式)
- [设计模式笔记]二. 结构型模式--9.Composite模式(组合)(一)
- 设计模式之结构型模式(待完成)
- .NET设计模式-结构型模式专题总结
- 设计模式(15)------解释器模式
- 设计模式---结构型
- 结构型设计模式
- 设计模式之结构型模式一
- 设计模式--结构型模式之六-Flyweight享元
- 23种设计模式(23)-解释器模式
- java-设计模式(结构型)-【代理模式】
- C#设计模式之十一享元模式(Flyweight Pattern)【结构型】