Interpreter Pattern

Category : Design Patterns | Sub Category : Behavioral Design Patterns | By Prasad Bonam Last updated: 2023-07-09 09:23:54 Viewed : 315


Interpreter Pattern:

The Interpreter pattern is a behavioral design pattern that defines a language and represents grammar rules using objects. It is used to interpret sentences or expressions in the defined language. The Interpreter pattern allows you to define a language, parse expressions in that language, and perform actions based on the parsed expressions. Here is an example of implementing the Interpreter pattern in Java:

java
// Abstract Expression interface Expression { boolean interpret(String context); } // Terminal Expression class TerminalExpression implements Expression { private String data; public TerminalExpression(String data) { this.data = data; } @Override public boolean interpret(String context) { return context.contains(data); } } // Nonterminal Expression class OrExpression implements Expression { private Expression expression1; private Expression expression2; public OrExpression(Expression expression1, Expression expression2) { this.expression1 = expression1; this.expression2 = expression2; } @Override public boolean interpret(String context) { return expression1.interpret(context) || expression2.interpret(context); } } // Nonterminal Expression class AndExpression implements Expression { private Expression expression1; private Expression expression2; public AndExpression(Expression expression1, Expression expression2) { this.expression1 = expression1; this.expression2 = expression2; } @Override public boolean interpret(String context) { return expression1.interpret(context) && expression2.interpret(context); } } // Client code public class Client { // Rules: John and Robert are male, Julie is a married female public static Expression getMaleExpression() { Expression john = new TerminalExpression("John"); Expression robert = new TerminalExpression("Robert"); return new OrExpression(john, robert); } public static Expression getMarriedFemaleExpression() { Expression julie = new TerminalExpression("Julie"); Expression married = new TerminalExpression("Married"); return new AndExpression(julie, married); } public static void main(String[] args) { Expression isMale = getMaleExpression(); Expression isMarriedFemale = getMarriedFemaleExpression(); System.out.println("John is male? " + isMale.interpret("John")); System.out.println("Julie is a married female? " + isMarriedFemale.interpret("Married Julie")); } }

In this example:

  • The Expression interface declares the interpret() method that is implemented by terminal and nonterminal expressions.
  • The TerminalExpression class represents a terminal expression, which performs a specific interpretation based on its data.
  • The OrExpression and AndExpression classes represent nonterminal expressions that combine multiple expressions using logical OR and AND operations, respectively.
  • The Client class demonstrates the usage of the interpreter pattern. It defines the grammar rules using expressions and creates specific expressions based on those rules. The client then interprets and performs actions based on the expressions.

By using the Interpreter pattern, you can define and interpret a language or expressions in that language. It provides a way to define complex grammars and perform actions based on the interpreted expressions. The pattern is useful when you need to evaluate sentences or expressions based on specific rules or criteria.

Search
Related Articles

Leave a Comment: