728x90
반응형
01.소스코드
01.1 App
import java.util.Map;
public class App {
public static void main(String[] args) {
PostfixExpression expression = PostfixParser.parse("xyz+-a+");
int result = expression.interpret(Map.of('x', 1, 'y', 2, 'z', 3, 'a', 4));
System.out.println(result);
}
}
01.2 PostfixExpression
import java.util.Map;
public interface PostfixExpression {
int interpret(Map<Character, Integer> context);
}
01.3 VariableExpression
import java.util.Map;
public class VariableExpression implements PostfixExpression {
private Character character;
public VariableExpression(Character character) {
this.character = character;
}
@Override
public int interpret(Map<Character, Integer> context) {
return context.get(this.character);
}
}
01.4 PlusExpression
public class PlusExpression implements PostfixExpression {
private PostfixExpression left;
private PostfixExpression 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);
}
}
01.5 MinusExpression
import java.util.Map;
public class MinusExpression implements PostfixExpression {
private PostfixExpression left;
private PostfixExpression 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);
}
}
01.6 PostfixParser
import java.util.Stack;
import static me.whiteship.designpatterns._03_behavioral_patterns._15_interpreter._02_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 new PlusExpression(stack.pop(), stack.pop());
case '-':
PostfixExpression right = stack.pop();
PostfixExpression left = stack.pop();
return new MinusExpression(left, right);
default:
return new VariableExpression(c);
}
}
}
02.다른 사용법
02.1 PostfixExpression
- before
-
import java.util.Map; public interface PostfixExpression { int interpret(Map<Character, Integer> context); }
- after
- 위와 같이 하면 Plus, Minus, Variable이 없어도됨
-
import java.util.Map; public interface PostfixExpression { int interpret(Map<Character, Integer> context); static PostfixExpression plus(PostfixExpression left, PostfixExpression right){ return new PostfixExpression(){ @Override public int interpret(Map<character, Integer> context){ return left.interpret(context) + right.interpret(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 variable(Character c){ return context -> context.get(c); } }
02.2 PostfixParser
- before
-
import java.util.Stack; import static me.whiteship.designpatterns._03_behavioral_patterns._15_interpreter._02_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 new PlusExpression(stack.pop(), stack.pop()); case '-': PostfixExpression right = stack.pop(); PostfixExpression left = stack.pop(); return new MinusExpression(left, right); default: return new VariableExpression(c); } } }
- after
-
import java.util.Stack; import static me.whiteship.designpatterns._03_behavioral_patterns._15_interpreter._02_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 new PostfixExpression.plus(stack.pop(), stack.pop()); case '-': PostfixExpression right = stack.pop(); PostfixExpression left = stack.pop(); return PostfixExpression.minus(left, right); default: return PostfixExpression.variable(c); } } }
728x90
반응형
'CS Study > 디자인패턴' 카테고리의 다른 글
2022-07-07-이터레이터패턴-1부-패턴소개 (0) | 2022.07.18 |
---|---|
2022-05-12-인터프리터패턴-3부-장점과단점 (0) | 2022.05.13 |
2022-05-12-인터프리터패턴-1부-패턴소개 (0) | 2022.05.12 |
2022-05-11-커맨드패턴-3부-장점과단점 (0) | 2022.05.12 |
2022-05-11-커맨드패턴-2부-패턴적용하기 (0) | 2022.05.12 |
댓글