카테고리 없음

[자바 디자인 패턴] 행동패턴 - 해석자 패턴

심심한 낙지 2019. 8. 12. 12:38

POOPOO: 배변 일기 앱

SMALL

행동패턴 목록


 

번역 패턴이기 때문에 특정 포맷을 파싱하는 경우에 사용할 수 있을 것 같지만, 주로 언어를 번역하는데 쓰인다고 한다. 아직은 어떤 용도로 써야할지 잘 모르겠다.

 

장점

 - 문법을 바꾸고 확장하는 것이 더 쉽습니다.

 - 문법을 구현하는 것은 간단합니다.

 

사용시기

 - 언어의 문법이 복잡하지 않은 경우

 - 효율성이 우선순위가 아닌 경우

 

UML

 

Interface Class : Expression

public interface Expression {
    public int interpret();
}

 

Expression Class : Product, Add, Substract, Number

public class Add implements Expression{
    private final Expression leftExpression;
    private final Expression rightExpression;
 
    public Add(Expression leftExpression,Expression rightExpression ){
        this.leftExpression = leftExpression;
        this.rightExpression = rightExpression;
    }
    
    @Override
    public int interpret() {
        return leftExpression.interpret() + rightExpression.interpret();
    }
}
public class Product implements Expression{
    private final Expression leftExpression;
    private final Expression rightExpression;
 
    public Product(Expression leftExpression,Expression rightExpression ){
        this.leftExpression = leftExpression;
        this.rightExpression = rightExpression;
    }
    
    @Override
    public int interpret() {
        return leftExpression.interpret() * rightExpression.interpret();
    }
}
public class Substract implements Expression{
    private final Expression leftExpression;
    private final Expression rightExpression;
 
    public Substract(Expression leftExpression,Expression rightExpression ){
        this.leftExpression = leftExpression;
        this.rightExpression = rightExpression;
    }
    
    @Override
    public int interpret() {
        return leftExpression.interpret() - rightExpression.interpret();
    }
}
public class Number implements Expression{
    private final int n;
     
    public Number(int n){
        this.n = n;
    }
    
    @Override
    public int interpret() {
        return n;
    }
}

 

Operation Class : ExpressionUtils, TestInterpreterPattern

public class ExpressionUtils {
    public static boolean isOperator(String s) {
        if (s.equals("+") || s.equals("-") || s.equals("*"))
            return true;
        else
            return false;
    }
     
    public static Expression getOperator(String s, Expression left, Expression right) {
        switch (s) {
        case "+":
            return new Add(left, right);
        case "-":
            return new Substract(left, right);
        case "*":
            return new Product(left, right);
        }
        return null;
    }
}
public class TestInterpreterPattern {
    public static void main(String[] args) {
        String tokenString = "7 3 - 2 1 + *";
        Stack<Expression> stack = new Stack<>();
        String[] tokenArray = tokenString.split(" ");
        
        for (String s : tokenArray) {
            if (ExpressionUtils.isOperator(s)) {
                Expression rightExpression = stack.pop();
                Expression leftExpression = stack.pop();
                Expression operator = ExpressionUtils.getOperator(s, leftExpression,rightExpression);
                int result = operator.interpret();
                stack.push(new Number(result));
            } else {
                Expression i = new Number(Integer.parseInt(s));
                stack.push(i);
            }
        }
        
        System.out.println("( "+tokenString+" ): "+stack.pop().interpret());
    }
}

 

출처 : https://www.javacodegeeks.com

 

Java Programming, Learn Java Online with the Java Code Geeks | Java developers resource center - Java, Scala, Groovy, Android

Early on in my agile practice, I believed in generalizing specialists. I even wrote Five Tips to Hiring a Generalizing Specialist. However, if a team becomes collaborative, I no longer think we need generalizing specialists. That’s because the team works a

www.javacodegeeks.com

LIST