- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Interpreter Pattern
Interpreter pattern provides a way to evaluate language grammar or expression. This type of pattern comes under behavioral pattern. This pattern involves implementing an expression interface which tells to interpret a particular context. This pattern is used in SQL parsing, symbol processing engine etc.
Implementation
We are going to create an interface Expression and concrete classes implementing the Expression interface. A class TerminalExpression is defined which acts as a main interpreter of context in question. Other classes OrExpression, AndExpression are used to create combinational expressions.
InterpreterPatternDemo, our demo class, will use Expression class to create rules and demonstrate parsing of expressions.
Step 1
Create an expression interface.
Expression.java
pubpc interface Expression { pubpc boolean interpret(String context); }
Step 2
Create concrete classes implementing the above interface.
TerminalExpression.java
pubpc class TerminalExpression implements Expression { private String data; pubpc TerminalExpression(String data){ this.data = data; } @Override pubpc boolean interpret(String context) { if(context.contains(data)){ return true; } return false; } }
OrExpression.java
pubpc class OrExpression implements Expression { private Expression expr1 = null; private Expression expr2 = null; pubpc OrExpression(Expression expr1, Expression expr2) { this.expr1 = expr1; this.expr2 = expr2; } @Override pubpc boolean interpret(String context) { return expr1.interpret(context) || expr2.interpret(context); } }
AndExpression.java
pubpc class AndExpression implements Expression { private Expression expr1 = null; private Expression expr2 = null; pubpc AndExpression(Expression expr1, Expression expr2) { this.expr1 = expr1; this.expr2 = expr2; } @Override pubpc boolean interpret(String context) { return expr1.interpret(context) && expr2.interpret(context); } }
Step 3
InterpreterPatternDemo uses Expression class to create rules and then parse them.
InterpreterPatternDemo.java
pubpc class InterpreterPatternDemo { //Rule: Robert and John are male pubpc static Expression getMaleExpression(){ Expression robert = new TerminalExpression("Robert"); Expression john = new TerminalExpression("John"); return new OrExpression(robert, john); } //Rule: Jupe is a married women pubpc static Expression getMarriedWomanExpression(){ Expression jupe = new TerminalExpression("Jupe"); Expression married = new TerminalExpression("Married"); return new AndExpression(jupe, married); } pubpc static void main(String[] args) { Expression isMale = getMaleExpression(); Expression isMarriedWoman = getMarriedWomanExpression(); System.out.println("John is male? " + isMale.interpret("John")); System.out.println("Jupe is a married women? " + isMarriedWoman.interpret("Married Jupe")); } }
Step 4
Verify the output.
John is male? true Jupe is a married women? trueAdvertisements