Skip to content

Commit b9e9383

Browse files
author
chrish
committed
Refactoring InterpreterPattern.
1 parent ac1fd7b commit b9e9383

3 files changed

Lines changed: 46 additions & 19 deletions

File tree

Behavioral/InterpreterPattern/src/io/csie/chris/Evaluator.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package io.csie.chris;
22

3-
import io.csie.chris.expression.Expression;
4-
import io.csie.chris.expression.Minus;
5-
import io.csie.chris.expression.Plus;
6-
import io.csie.chris.expression.Variable;
3+
import io.csie.chris.expression.*;
74

85
import java.util.Map;
96
import java.util.Stack;
@@ -12,23 +9,32 @@ public class Evaluator implements Expression {
129

1310
private Expression syntaxTree;
1411

15-
public Evaluator(final String expression) {
12+
Evaluator(final String expression) {
1613

17-
final Stack<Expression> expressionStack = new Stack<Expression>();
14+
final Stack<Expression> expressionStack = new Stack<>();
1815

1916
for (final String token : expression.split(" ")) {
20-
if (token.equals("+")) {
21-
final Expression subExpression = new Plus(expressionStack.pop(), expressionStack.pop());
22-
expressionStack.push(subExpression);
23-
} else if (token.equals("-")) {
24-
// it's necessary remove first the right operand from the stack
25-
final Expression right = expressionStack.pop();
26-
// ..and after the left one
27-
final Expression left = expressionStack.pop();
28-
final Expression subExpression = new Minus(left, right);
29-
expressionStack.push(subExpression);
30-
} else
31-
expressionStack.push(new Variable(token));
17+
switch (token) {
18+
case "+":
19+
final Expression addExpression = new Plus(expressionStack.pop(), expressionStack.pop());
20+
expressionStack.push(addExpression);
21+
break;
22+
case "-":
23+
// it's necessary remove first the right operand from the stack
24+
final Expression right = expressionStack.pop();
25+
// ..and after the left one
26+
final Expression left = expressionStack.pop();
27+
final Expression subExpression = new Minus(left, right);
28+
expressionStack.push(subExpression);
29+
break;
30+
case "*":
31+
final Expression multiExpression = new Multiplication(expressionStack.pop(), expressionStack.pop());
32+
expressionStack.push(multiExpression);
33+
break;
34+
default:
35+
expressionStack.push(new Variable(token));
36+
break;
37+
}
3238
}
3339
syntaxTree = expressionStack.pop();
3440
}

Behavioral/InterpreterPattern/src/io/csie/chris/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ public class Main {
1010

1111
public static void main(final String[] args) {
1212

13-
final String expression = "w x z - +";
13+
final String expression = "v w x z - + *";
1414

1515
final Evaluator sentence = new Evaluator(expression);
1616

1717
final Map<String, Expression> variables = new HashMap<>();
1818

19+
variables.put("v", new Number(2));
1920
variables.put("w", new Number(5));
2021
variables.put("x", new Number(10));
2122
variables.put("z", new Number(42));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.csie.chris.expression;
2+
3+
import java.util.Map;
4+
5+
public class Multiplication implements Expression{
6+
7+
private Expression leftOperand;
8+
9+
private Expression rightOperand;
10+
11+
public Multiplication(Expression leftOperand, Expression rightOperand) {
12+
this.leftOperand = leftOperand;
13+
this.rightOperand = rightOperand;
14+
}
15+
16+
@Override
17+
public int interpret(Map<String, Expression> variables) {
18+
return leftOperand.interpret(variables) * rightOperand.interpret(variables);
19+
}
20+
}

0 commit comments

Comments
 (0)