Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Design Patterns
|
Interpreter
References
Example
The
Context
The
Interpreter
The
Concrete Interpreter
(1)
The
Concrete Interpreter
(2)
The
Concrete Interpreter
(2-1)
The
Concrete Interpreter
(2-2)
A simple class to test the
Interpreter
design pattern
References
Definition: (
source:
http://en.wikipedia.org/wiki/Interpreter_pattern
)
The intent of the interpreter pattern is to specify how to evaluate sentences in a language.
Class diagram: (
source:
http://en.wikipedia.org/wiki/Interpreter_pattern
)
Example
The
Context
:
public class Variables { private final Map<String, Boolean> variables = new HashMap<String, Boolean>(); public void addVariable(final String variable, final Boolean value) { variables.put(variable, value); } public Boolean getVariable(final String variable) { return variables.get(variable); } }
The
Interpreter
:
public interface Expression { Boolean interpret(final Variables variables); }
The
Concrete Interpreter
(1):
public class TerminalExpression implements Expression { private final String variable; public TerminalExpression(final String variable) { this.variable = variable; } public String getVariable() { return variable; } public Boolean interpret(final Variables variables) { return variables.getVariable(variable); } }
The
Concrete Interpreter
(2):
public abstract class NonTerminalExpression implements Expression { private Expression leftExpression; private Expression rightExpression; public NonTerminalExpression(final Expression leftExpression, final Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; } protected Expression getLeftExpression() { return leftExpression; } protected Expression getRightExpression() { return rightExpression; } }
The
Concrete Interpreter
(2-1):
public class AndExpression extends NonTerminalExpression { public AndExpression(final Expression leftExpression, final Expression rightExpression) { super(leftExpression, rightExpression); } public Boolean interpret(final Variables variables) { return getLeftExpression().interpret(variables) && getRightExpression().interpret(variables); } }
The
Concrete Interpreter
(2-2):
public class OrExpression extends NonTerminalExpression { public OrExpression(final Expression leftExpression, final Expression rightExpression) { super(leftExpression, rightExpression); } public Boolean interpret(final Variables variables) { return getLeftExpression().interpret(variables) || getRightExpression().interpret(variables); } }
A simple class to test the
Interpreter
design pattern:
public class InterpreterPatternTest { public static void main(String[] args) { final String variable1 = "a"; final String variable2 = "b"; final String variable3 = "c"; final String variable4 = "d"; // Context creation final Variables variables = new Variables(); variables.addVariable(variable1, false); variables.addVariable(variable2, true); variables.addVariable(variable3, true); variables.addVariable(variable4, true); // Concrete Interpreter final TerminalExpression terminalExpression1 = new TerminalExpression(variable1); final TerminalExpression terminalExpression2 = new TerminalExpression(variable2); final TerminalExpression terminalExpression3 = new TerminalExpression(variable3); final TerminalExpression terminalExpression4 = new TerminalExpression(variable4); // Concrete Interpreter: test 1 final Expression andExpression1 = new AndExpression(terminalExpression1, terminalExpression2); System.out.println(andExpression1.interpret(variables)); // Concrete Interpreter: test 2 final Expression andExpression2 = new AndExpression(terminalExpression3, andExpression1); System.out.println(andExpression2.interpret(variables)); // Concrete Interpreter: test 3 final Expression orExpression1 = new OrExpression(terminalExpression4, andExpression2); System.out.println(orExpression1.interpret(variables)); } }
© 2010-2022
mti
tek