Interpreter Design Pattern
Hibernate Core Java
Question:- What is Interpreter Design Pattern?
This design pattern falls under Behavioral design pattern. This pattern provides a way to evaluate language expression. This design pattern involves in implementing an expression interface which tells to interpret a particular context.
As per GOF :-“Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.”
Question:- When to use Interpreter design pattern?
This design pattern is used for specialized computer languages which are often used to describe communication protocols.This design pattern is also used in symbol processing engine, SQL parsing etc.
- Examples in JDK API
- Live examples of Interpreter design pattern
- Google Translator is an example of interpreter pattern where the input can be in any language and we can get the output interpreted in another language.
- In java itself, this design pattern is implemented where java compiler interprets the java source code into byte code that is understandable by JVM.
Drawback
During implementation of Interpreter design pattern, it requires large piece of code for error checking and expression evaluation. So when the grammer is complicated then the required piece of code is complicated and hence it is very difficult to maintain and provide efficient code.
In the below example,we will see a sample of using interpreter design pattern.
InterpreterImplemeter.java
package com.gaurav.designpattern.interpreter;
/**
* @author Gaurav
*
*/
public class InterpreterImplemeter {
public String getBinaryConversion(int number) {
return Integer.toBinaryString(number);
}
public String getHexadecimalConversion(int number) {
return Integer.toHexString(number);
}
}
ExpressionConversion.java
package com.gaurav.designpattern.interpreter;
/**
* @author Gaurav
*
*/
public interface ExpressionConversion {
public String interpretData(InterpreterImplemeter interpreter);
}
IntegerToBinary.java
package com.gaurav.designpattern.interpreter;
/**
* @author Gaurav
*
*/
public class IntegerToBinary implements ExpressionConversion{
private int number;
public IntegerToBinary(int num){
this.number = num;
}
@Override
public String interpretData(InterpreterImplemeter interpreter) {
return interpreter.getBinaryConversion(this.number);
}
}
IntegerToHexadecimal.java
package com.gaurav.designpattern.interpreter;
/**
* @author Gaurav
*
*/
public class IntegerToHexadecimal implements ExpressionConversion{
private int number;
public IntegerToHexadecimal(int num){
this.number = num;
}
@Override
public String interpretData(InterpreterImplemeter interpreter) {
return interpreter.getHexadecimalConversion(this.number);
}
}
InterpreterDesignPatternDemo.java
package com.gaurav.designpattern.interpreter;
import java.util.Scanner;
/**
* @author Gaurav
*
*/
public class InterpreterDesignPatternDemo {
public InterpreterImplemeter interPreter;
public InterpreterDesignPatternDemo(InterpreterImplemeter i) {
this.interPreter = i;
}
public String binaryConverter(int num) {
ExpressionConversion expressionConverter = new IntegerToHexadecimal(num);
return expressionConverter.interpretData(interPreter);
}
public String hexadecimalConverter(int num) {
ExpressionConversion expressionConverter = new IntegerToBinary(num);
return expressionConverter.interpretData(interPreter);
}
public static void main(String args[]) {
InterpreterDesignPatternDemo client = new InterpreterDesignPatternDemo(
new InterpreterImplemeter());
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the conversion type either binary or hexa : ");
String conversionType = scanner.nextLine();
System.out.println("Enter the number for conversion : ");
int number = scanner.nextInt();
if("binary".equalsIgnoreCase(conversionType))
System.out.println(number + " in binary is : " + client.binaryConverter(number));
else if("hexa".equalsIgnoreCase(conversionType))
System.out.println(number + " in hexadecimal is : " + client.hexadecimalConverter(number));
else{
System.out.println("Unable to convert, please provide correct conversion type as mentioned.");
}
}
}
Result : -
1st Execution :
Enter the conversion type either binary or hexa :
binary
Enter the number for conversion :
10
10 in binary is : a
2nd Execution :
Enter the conversion type either binary or hexa :
hexa
Enter the number for conversion :
10
10 in hexadecimal is : 1010
3rd Execution :
Enter the conversion type either binary or hexa :
octa
Enter the number for conversion :
10
Unable to convert, please provide correct conversion type as mentioned.