这种模式实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等。
abstract public class Expression {
public abstract int interpreter(Map<String, Integer> var);
}
public class SymbolExpression extends Expression {
protected Expression left, right;
public SymbolExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public int interpreter(Map<String, Integer> var) {
return 0;
}
}
public class AddExpression extends SymbolExpression {
public AddExpression(Expression left, Expression right) {
super(left, right);
}
@Override
public int interpreter(Map<String, Integer> var) {
return super.left.interpreter(var) + super.right.interpreter(var);
}
}
public class SubExpression extends SymbolExpression {
public SubExpression(Expression left, Expression right) {
super(left, right);
}
@Override
public int interpreter(Map<String, Integer> var) {
return super.left.interpreter(var) - super.right.interpreter(var);
}
}
public class VarExpression extends Expression {
private String key;
public VarExpression(String key) {
this.key = key;
}
/**
* 根据变量的名称返回对应的值
* @param var
* @return
*/
@Override
public int interpreter(Map<String, Integer> var) {
return var.get(this.key);
}
}
public class Calculator {
private Expression expression;
public Calculator(String expStr) {
Stack<Expression> stack = new Stack<>();
char[] chars = expStr.toCharArray();
Expression left, right;
for (int i = 0; i < chars.length; i++) {
switch (chars[i]) {
case '+':
left = stack.pop();
right = new VarExpression(chars[++i]+"");
stack.push(new AddExpression(left, right));
break;
case '-':
left = stack.pop();
right = new VarExpression(chars[++i]+"");
stack.push(new SubExpression(left, right));
break;
default:
stack.push(new VarExpression(chars[i]+""));
break;
}
}
this.expression = stack.pop();
}
public int run(Map<String, Integer> var) {
return this.expression.interpreter(var);
}
}
public class Client {
public static void main(String[] args) throws IOException {
String expStr = getExpStr();
Map<String, Integer> var = getValue(expStr);
Calculator calculator = new Calculator(expStr);
System.out.println("result: " + expStr + " = " + calculator.run(var));
}
public static String getExpStr() throws IOException {
System.out.println("Type in an expression.");
return (new BufferedReader(new InputStreamReader(System.in))).readLine();
}
public static Map<String, Integer> getValue(String expStr) throws IOException {
Map<String, Integer> map = new HashMap<>();
for (char ch : expStr.toCharArray()) {
if (!(ch == '+' || ch == '-')) {
if (!map.containsKey(ch+"")) {
System.out.println("input " +ch+"'s value: ");
String in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
map.put(ch+"", Integer.valueOf(in));
}
}
}
return map;
}
}
Type in an expression.
w+r-y
input w's value:
5
input r's value:
6
input y's value:
3
result: w+r-y = 8
Process finished with exit code 0
- https://www.runoob.com/design-pattern/interpreter-pattern.html
- https://pan.baidu.com/s/1VipQD0LaJjbSEZevg9K1-g
- 韩顺平 《设计模式》
原型模式(Prototype Pattern)
抽象工厂模式(Abstract Factory Pattern)
建造者模式(Builder Pattern)
工厂模式(Factory Pattern)
单例模式(Singleton Pattern)
助记语:原抽建工单
享元模式(Flyweight Pattern)
代理模式(Proxy Pattern)
适配器模式(Adapter Pattern)
外观模式(Facade Pattern)
过滤器模式(Filter/Criteria Pattern)
桥接模式(Bridge Pattern)
组合模式(Composite Pattern)
装饰器模式(Decorator Pattern)
助记语:想呆室外,过桥组装
责任链模式(Chain of Responsibility Pattern)
命令模式(Command Pattern)
解释器模式(Interpreter Pattern)
中介者模式(Mediator Pattern)
迭代器模式(Iterator Pattern)
观察者模式(Observer Pattern)
策略模式(Strategy Pattern)
状态模式(State Pattern)
备忘录模式(Memento Pattern)
模板方法模式(Template Pattern)
访问者模式(Visitor Pattern)
助记语:责令解中谍,观测状被模仿