Interpreter pattern

Interpreter Pattern

Interpreter pattern is design technique which specifies how can evaluate sentences in language. Actually basic idea is to create separate class for each symbol using specialized computer language. Built syntax tree of sentence in the language will be instance of composite pattern that will be used to evaluate (interpret) sentence.

Below are uses of Interpreter pattern:

  • It also used to describe communication protocols using specialized computer languages
  • Database SQL (Specialized database query languages)
  • Utmost all general computer languages incorporated by several specialized languages.

Example:

  • Create Expression Interface:
import java.util.Map;

public interface Expression {
	public int interpret(Map<String,Expression> variables);
}

 

  • Class Number
import java.util.Map;

public class Number implements Expression {
	private int number;

	public Number(int number) {
		this.number = number;
	}

	@Override
	public int interpret(Map<String, Expression> variables) {
		return number;
	}
}

 

  • Class Plus
import java.util.Map;

public class Plus implements Expression {
	Expression leftOperand;
	Expression rightOperand;

	public Plus(Expression left, Expression right) {
		leftOperand = left;
		rightOperand = right;
	}

	public int interpret(Map<String, Expression> variables) {
		return leftOperand.interpret(variables)
				+ rightOperand.interpret(variables);
	}
}

 

  • Class Minus
import java.util.Map;

public class Minus implements Expression {
	Expression leftOperand;
	Expression rightOperand;

	public Minus(Expression left, Expression right) {
		leftOperand = left;
		rightOperand = right;
	}

	public int interpret(Map<String, Expression> variables) {
		return leftOperand.interpret(variables)
				- rightOperand.interpret(variables);
	}
}

 

  • Class Variable
import java.util.Map;

public class Variable implements Expression {
	private String name;

	public Variable(String name) {
		this.name = name;
	}

	public int interpret(Map<String, Expression> variables) {
		if (null == variables.get(name))
			return 0; // Either return new Number(0).
		return variables.get(name).interpret(variables);
	}
}

 

  • Class Evaluator
import java.util.Map;
import java.util.Stack;

public class Evaluator implements Expression {
	private Expression syntaxTree;

	public Evaluator(String expression) {
		Stack<Expression> expressionStack = new Stack<Expression>();
		for (String token : expression.split(" ")) {
			if (token.equals("+")) {
				Expression subExpression = new Plus(expressionStack.pop(),
						expressionStack.pop());
				expressionStack.push(subExpression);
			} else if (token.equals("-")) {
				// it's necessary remove first the right operand from the stack
				Expression right = expressionStack.pop();
				// ..and after the left one
				Expression left = expressionStack.pop();
				Expression subExpression = new Minus(left, right);
				expressionStack.push(subExpression);
			} else
				expressionStack.push(new Variable(token));
		}
		syntaxTree = expressionStack.pop();
	}

	public int interpret(Map<String, Expression> context) {
		return syntaxTree.interpret(context);
	}
}

 

  • Test Class InterpreterExample
import java.util.HashMap;
import java.util.Map;

public class InterpreterExample {
	public static void main(String[] args) {
		String expression = "w x z - +";
		Evaluator sentence = new Evaluator(expression);
		Map<String, Expression> variables = new HashMap<String, Expression>();
		variables.put("w", new Number(5));
		variables.put("x", new Number(10));
		variables.put("z", new Number(42));
		int result = sentence.interpret(variables);
		System.out.println(result);
	}
}
  • That’s it. Below is list of all design patterns link:

Creational Patterns:

Structural Patterns:

Behavioral Patterns:

Leave a Reply

Your email address will not be published. Required fields are marked *