<p><strong>Interpreter pattern</strong> provides a way to evaluate language grammar or expression. The term interpreter means a person who interprets the stuff in a foreign language into a language that is understandable. In the terms of computer programming, an interpreter is a pattern in which the programming language is evaluated line by line. This type of pattern comes under <strong><a href="https://dineshonjava.com/behavioral-design-pattern/">behavioral pattern</a></strong> of the <strong><a href="https://dineshonjava.com/design-patterns_25/">23 GoF Design Patterns</a></strong>.</p>
<h2>Interpreter pattern</h2>
<div style="text-align: left;">
<blockquote><p><strong>According to the Gang of Four:</strong></p>
<div style="text-align: left;">Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.</div>
</blockquote>
</div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Pattern</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern" target="_blank" rel="noopener"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns" target="_blank" rel="noopener"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput" target="_blank" rel="noopener"><img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern-243x300.jpg" width="243" height="300" /></a></div>
</div>
<p>Behavioral pattern is a design pattern through which common communication patterns are identified between different objects. This way the communication is carried out in more flexible way. The areas of use of this design pattern of the interpreter are symbol processing engine, SQL parsing etc.</p>
<div style="text-align: left;">
<blockquote><p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/observer-pattern-design-patterns-java/" href="https://dineshonjava.com/observer-pattern-design-patterns-java/">Observer Design Pattern</a></li>
<li><a title="https://dineshonjava.com/chain-responsibility-pattern/" href="https://dineshonjava.com/chain-responsibility-pattern/">Chain of responsibility Pattern</a></li>
<li><a title="https://dineshonjava.com/command-pattern-design-patterns-java/" href="https://dineshonjava.com/command-pattern-design-patterns-java/">Command Design Pattern</a></li>
<li><a title="https://dineshonjava.com/strategy-pattern-design-patterns-java/" href="https://dineshonjava.com/strategy-pattern-design-patterns-java/">Strategy Design Pattern</a></li>
<li><a title="https://dineshonjava.com/template-method-pattern-design-java/" href="https://dineshonjava.com/template-method-pattern-design-java/">Template method Design Pattern</a></li>
<li><a title="https://dineshonjava.com/iterator-pattern-design-patterns-java/" href="https://dineshonjava.com/iterator-pattern-design-patterns-java/">Iterator Design Pattern</a></li>
<li><a title="https://dineshonjava.com/mediator-pattern-design-patterns-java/" href="https://dineshonjava.com/mediator-pattern-design-patterns-java/">Mediator Design Pattern</a></li>
<li><a title="https://dineshonjava.com/memento-pattern-design-patterns-java/" href="https://dineshonjava.com/memento-pattern-design-patterns-java/">Memento Design Pattern</a></li>
<li><a title="https://dineshonjava.com/creational-design-patterns/" href="https://dineshonjava.com/creational-design-patterns//">Creational GoF Design Patterns</a></li>
<li><a title="https://dineshonjava.com/structural-design-patterns/" href="https://dineshonjava.com/structural-design-patterns/">Structural GoF Design Patterns</a></li>
</ul>
</blockquote>
</div>
<h2>UML class diagram for Interpreter Design Pattern</h2>
<p>There is a following UML diagram illustrates about this patterns and its components.</p>
<h2><img class="aligncenter wp-image-3475 size-full" src="https://dineshonjava.com/wp-content/uploads/2017/12/interpreter.gif" alt="Interpreter Pattern " width="404" height="266" /><br />
Benefits of Interpreter Pattern</h2>
<p>The following lists the benefits of using the Interpreter pattern:</p>
<ul>
<li>This pattern allows you to change and extend the grammar easily.</li>
<li>Using the expression language is very easy</li>
</ul>
<h2>Steps to Implementation for this Pattern</h2>
<p>Here are the following steps for the implementation of the interpreter.</p>
<h3>Step 1: AbstractExpression (Expression)</h3>
<p>The first step for the implementation of the interpreter is the creation of the expression interface “Expression.java”.</p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.behavior.interpreter; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public abstract class Expression { 
	 
	public void Interpret(Context context){ 
		if (context.get_input().length() == 0) 
			return; 
		if (context.get_input().startsWith(nine())) { 
			context.set_output(context.get_output()+ 9 * multiplier()); 
			context.set_input(context.get_input().substring(2)); 
		}else if (context.get_input().startsWith(four())){ 
			context.set_output(context.get_output()+ 4 * multiplier()); 
			context.set_input(context.get_input().substring(2)); 
		}else if (context.get_input().startsWith(five())){ 
			context.set_output(context.get_output()+ 5 * multiplier()); 
			context.set_input(context.get_input().substring(1)); 
		} 
 
		while (context.get_input().startsWith(one())){ 
			context.set_output(context.get_output()+ 1 * multiplier()); 
			context.set_input(context.get_input().substring(1)); 
		} 
	} 
 
	public abstract String one(); 
 
 public abstract String four(); 
 
 public abstract String five(); 
 
 public abstract String nine(); 
 
 public abstract int multiplier(); 
} 
 
</pre>
<h3>Step 2: TerminalExpression ( ThousandExpression, HundredExpression, TenExpression, OneExpression )</h3>
<p>After the creation of the interface or abstract, the second step is to use the created interface to create concrete classes.</p>
<p><strong>ThousandExpression.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.behavior.interpreter; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class ThousandExpression extends Expression { 
 
	@Override 
	public String one() { 
		return "M"; 
	} 
 
	@Override 
	public String four() { 
		return " "; 
	} 
 
	@Override 
	public String five() { 
		return " "; 
	} 
 
	@Override 
	public String nine() { 
		return " "; 
	} 
 
	@Override 
	public int multiplier() { 
		return 1000; 
	} 
} 
 
</pre>
<p><strong>HundredExpression.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.behavior.interpreter; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class HundredExpression extends Expression { 
 
	@Override 
	public String one() { 
		return "C"; 
	} 
 
	@Override 
	public String four() { 
		return "CD"; 
	} 
 
	@Override 
	public String five() { 
		return "D"; 
	} 
 
	@Override 
	public String nine() { 
		return "CM"; 
	} 
 
	@Override 
	public int multiplier() { 
		return 100; 
	} 
 
} 
 
</pre>
<p><strong>TenExpression.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.behavior.interpreter; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class TenExpression extends Expression { 
 
	@Override 
	public String one() { 
		return "X"; 
	} 
 
	@Override 
	public String four() { 
		return "XL"; 
	} 
 
	@Override 
	public String five() { 
		return "L"; 
	} 
 
	@Override 
	public String nine() { 
		return "XC"; 
	} 
 
	@Override 
	public int multiplier() { 
		return 10; 
	} 
 
} 
 
</pre>
<p><strong>OneExpression.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.behavior.interpreter; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class OneExpression extends Expression { 
 
	@Override 
	public String one() { 
		return "I"; 
	} 
 
	@Override 
	public String four() { 
		return "IV"; 
	} 
 
	@Override 
	public String five() { 
		return "V"; 
	} 
 
	@Override 
	public String nine() { 
		return "IX"; 
	} 
 
	@Override 
	public int multiplier() { 
		return 1; 
	} 
 
} 
 
</pre>
<h3>Step 3: Context (Context.java)</h3>
<p>It contains information that is global to the interpreter</p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.behavior.interpreter; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class Context { 
	private String _input; 
	private int _output; 
	 
	public Context(String _input) { 
		super(); 
		this._input = _input; 
	} 
	public String get_input() { 
		return _input; 
	} 
	public void set_input(String _input) { 
		this._input = _input; 
	} 
	public int get_output() { 
		return _output; 
	} 
	public void set_output(int _output) { 
		this._output = _output; 
	} 
	 
} 
 
</pre>
<h3>Step 4: Let&#8217;s create Interpreterpatterndemo.java file using the expression class to create rules and then parse them.</h3>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.behavior.interpreter; 
 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class InterpreterPatternDemo { 
 
	/** 
	 * @param args 
	 */ 
	public static void main(String[] args) { 
		String roman = "MMCDXXVIIVI"; 
		Context context = new Context(roman); 
 
		// Build the 'parse tree' 
		List tree = new ArrayList<;>;(); 
		tree.add(new ThousandExpression()); 
		tree.add(new HundredExpression()); 
		tree.add(new TenExpression()); 
		tree.add(new OneExpression()); 
		 
		// Interpret 
		for (Expression exp : tree){ 
			exp.Interpret(context); 
		} 
		System.out.println(roman+" = "+context.get_output()); 
	} 
 
} 
 
</pre>
<h3>Step 5: The fifth and the final step is to verify the output.</h3>
<pre class="highlight">MMCDXXVIIVI = 2427 
</pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/memento-pattern-design-patterns-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/command-pattern-design-patterns-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '3474'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…