design patterns : interpreter
This commit is contained in:
17
design-pattern/gof/src/interpreter/after/App.java
Normal file
17
design-pattern/gof/src/interpreter/after/App.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package interpreter.after;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
PostfixExpression expression = PostfixParser.parse("xyz+-");
|
||||
Map<Character, Integer> map = new HashMap<>();
|
||||
map.put('x', 1);
|
||||
map.put('y', 2);
|
||||
map.put('z', 3);
|
||||
int result = expression.interpret(map);
|
||||
System.out.println("result = " + result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package interpreter.after;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class MinusExpression implements PostfixExpression {
|
||||
|
||||
private PostfixExpression left, right;
|
||||
|
||||
public MinusExpression(PostfixExpression left, PostfixExpression right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int interpret(Map<Character, Integer> context) {
|
||||
return left.interpret(context) - right.interpret(context);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package interpreter.after;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class MultiplyExpression implements PostfixExpression {
|
||||
|
||||
private PostfixExpression left, right;
|
||||
|
||||
public MultiplyExpression(PostfixExpression left, PostfixExpression right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int interpret(Map<Character, Integer> context) {
|
||||
return left.interpret(context) * right.interpret(context);
|
||||
}
|
||||
}
|
||||
18
design-pattern/gof/src/interpreter/after/PlusExpression.java
Normal file
18
design-pattern/gof/src/interpreter/after/PlusExpression.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package interpreter.after;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class PlusExpression implements PostfixExpression {
|
||||
|
||||
private PostfixExpression left, right;
|
||||
|
||||
public PlusExpression(PostfixExpression left, PostfixExpression right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int interpret(Map<Character, Integer> context) {
|
||||
return left.interpret(context) + right.interpret(context);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package interpreter.after;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface PostfixExpression {
|
||||
|
||||
int interpret(Map<Character, Integer> context);
|
||||
|
||||
static PostfixExpression plus(PostfixExpression left, PostfixExpression right) {
|
||||
return context -> left.interpret(context) + right.interpret(context);
|
||||
}
|
||||
|
||||
static PostfixExpression minus(PostfixExpression left, PostfixExpression right) {
|
||||
return context -> left.interpret(context) - right.interpret(context);
|
||||
}
|
||||
|
||||
static PostfixExpression multiply(PostfixExpression left, PostfixExpression right) {
|
||||
return context -> left.interpret(context) * right.interpret(context);
|
||||
}
|
||||
|
||||
static PostfixExpression variable(Character c) {
|
||||
return context -> context.get(c);
|
||||
}
|
||||
}
|
||||
31
design-pattern/gof/src/interpreter/after/PostfixParser.java
Normal file
31
design-pattern/gof/src/interpreter/after/PostfixParser.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package interpreter.after;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
import static interpreter.after.PostfixExpression.*;
|
||||
|
||||
public class PostfixParser {
|
||||
|
||||
public static PostfixExpression parse(String expression) {
|
||||
Stack<PostfixExpression> stack = new Stack<>();
|
||||
for (char c : expression.toCharArray()) {
|
||||
stack.push(getExpression(c, stack));
|
||||
}
|
||||
return stack.pop();
|
||||
}
|
||||
|
||||
private static PostfixExpression getExpression(char c, Stack<PostfixExpression> stack) {
|
||||
switch (c) {
|
||||
case '+':
|
||||
return plus(stack.pop(), stack.pop());
|
||||
case '-':
|
||||
PostfixExpression right = stack.pop();
|
||||
PostfixExpression left = stack.pop();
|
||||
return minus(left, right);
|
||||
case '*':
|
||||
return multiply(stack.pop(), stack.pop());
|
||||
default:
|
||||
return variable(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package interpreter.after;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class VariableExpression implements PostfixExpression {
|
||||
|
||||
private Character variable;
|
||||
|
||||
public VariableExpression(Character variable) {
|
||||
this.variable = variable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int interpret(Map<Character, Integer> context) {
|
||||
return context.get(variable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package interpreter.before;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class PostfixNotation {
|
||||
|
||||
private final String expression;
|
||||
|
||||
public PostfixNotation(String expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PostfixNotation postfixNotation = new PostfixNotation("123+-");
|
||||
postfixNotation.calculate();
|
||||
}
|
||||
|
||||
private void calculate() {
|
||||
Stack<Integer> numbers = new Stack<>();
|
||||
|
||||
for (char c : this.expression.toCharArray()) {
|
||||
switch (c) {
|
||||
case '+':
|
||||
numbers.push(numbers.pop() + numbers.pop());
|
||||
break;
|
||||
case '-':
|
||||
int right = numbers.pop();
|
||||
int left = numbers.pop();
|
||||
numbers.push(left - right);
|
||||
break;
|
||||
default:
|
||||
numbers.push(Integer.parseInt(c + ""));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(numbers.pop());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user