「Interpreter」という英単語は、「通訳」を意味します。
このパターンは、なんらかの文法規則をもった文書(プログラム言語)を解析し、その結果得られた手順(命令)に基づき処理を実行していくというパターンです。
Interpreterパターンのクラス図
1. AbstractExpression.java
public abstract class AbstractExpression { public abstract int Interpret(Context context); }
2. TerminalExpression.java
public class TerminalExpression extends AbstractExpression{ private String saveToken; public int Interpret(Context context) { String token = context.getToken(); saveToken = token; context.nextToken(); return Integer.parseInt(token); } public String toString(){ return saveToken; } }
3. NonterminalExpression.java
import java.util.List; import java.util.ArrayList; public class NonterminalExpression extends AbstractExpression { private int resultValue; private String startTagPlus = "<+>"; private String endTagPlus = "</+>"; private List<AbstractExpression> list = new ArrayList<AbstractExpression>(); public int Interpret(Context context) { AbstractExpression childExpressions; context.nextToken(); String token = ""; while (!context.isEnd()) { token = context.getToken(); if (startTagPlus.equals(token)) { childExpressions = new NonterminalExpression(); } else if (endTagPlus.equals(token)) { context.nextToken(); break; } else { childExpressions = new TerminalExpression(); } resultValue += childExpressions.Interpret(context); list.add(childExpressions); } return resultValue; } public String toString(){ return "+" + list.toString(); } }
4. Context.java
import java.util.StringTokenizer; public class Context { private StringTokenizer tokens; private String currentToken; public Context(String source) { tokens = new StringTokenizer(source); nextToken(); } public String getToken(){ return currentToken; } public void nextToken(){ String token = null; if (!isEnd()){ token = tokens.nextToken(); } currentToken = token; } public boolean isEnd(){ return !tokens.hasMoreElements(); } }
5. Client.java
public class Client { public static void main(String[] args) { String sourse = "<+> 10 <+> 1 4 8 5 1 <+> 1 7 </+> 1 </+> 10 1 </+>"; Context context = new Context(sourse); AbstractExpression expression = new NonterminalExpression(); System.out.println(expression.Interpret(context)); System.out.println(expression.toString()); } }
C:\sample\desin_pattern\interpreter>javac Client.java [Enter] C:\sample\desin_pattern\interpreter>java Client [Enter] 49 +[10, +[1, 4, 8, 5, 1, +[1, 7], 1], 10, 1]