2022-05-12-인터프리터패턴-2부-패턴적용하기
본문 바로가기
CS Study/디자인패턴

2022-05-12-인터프리터패턴-2부-패턴적용하기

by KyeongMin 2022. 5. 13.
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);
            }
        }
    }

https://github.com/3DPIT/3dpit.github.io/blob/main/content/blog/DesignPattern/2022-05-12-%EC%9D%B8%ED%84%B0%ED%94%84%EB%A6%AC%ED%84%B0%ED%8C%A8%ED%84%B4-2%EB%B6%80-%ED%8C%A8%ED%84%B4%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0.md

 

GitHub - 3DPIT/3dpit.github.io

Contribute to 3DPIT/3dpit.github.io development by creating an account on GitHub.

github.com

 

728x90
반응형

댓글