一、定义

给定一种语言,定义它的语法的一种表示和一个解释器,该解释器使用该表示来解释语言中的句子。

二、类图表示

解释器模式

三、实现

例子程序是将罗马数字转换为阿拉伯数字。

1、Context

package pattern.behavioral.interpreter;

public class Context {

	private String input;
	private int output;
	
	public Context(String input) {
		this.input = input;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}

	public int getOutput() {
		return output;
	}

	public void setOutput(int output) {
		this.output = output;
	}
}

2、AbstractExpression

package pattern.behavioral.interpreter;

public abstract class Expression {

	public void interpret(Context context){
		String input = context.getInput();
		if(input == null || "".equals(input.trim())){
			return;
		}
		int result = context.getOutput();
		String nine = nine();
		String four = four();
		String five = five();
		String one = one();
		int multiplier = multiplier();
		//IV、VI等先判断
		if(input.startsWith(nine)){
			result += 9 * multiplier;
			input = input.substring(nine.length());//substring(2)
		}else if(input.startsWith(four)){
			result += 4 * multiplier;
			input = input.substring(four.length());//substring(2)
		}else if(input.startsWith(five)){
			result += 5 * multiplier;
			input = input.substring(five.length());//substring(1)
		}
		//I、X、C可能会出现3次
		while(input.startsWith(one)){
			result += multiplier;
			input = input.substring(one.length());//substring(1)
		}
		context.setInput(input);
		context.setOutput(result);
	}
	
	public abstract String one();
	public abstract String four();
	public abstract String five();
	public abstract String nine();
	public abstract int multiplier();
}

3、ConcreteExpression

//个位数解析
package pattern.behavioral.interpreter;

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;
	}

}


//十位数解析
package pattern.behavioral.interpreter;

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;
	}

}


//百位数解析
package pattern.behavioral.interpreter;

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;
	}

}


//千位数解析
package pattern.behavioral.interpreter;

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;
	}

}

四、使用

package pattern.behavioral.interpreter;

import java.util.ArrayList;
import java.util.List;

public class Client {

	public static void main(String[] args) {
		String romanNumber = "MMMCDLXXVIII";
		Context context = new Context(romanNumber);
		
		List<Expression> parsers = new ArrayList<>();
		parsers.add(new ThousandExpression());
		parsers.add(new HundredExpression());
		parsers.add(new TenExpression());
		parsers.add(new OneExpression());
		
		for(Expression parser : parsers){
			parser.interpret(context);
		}
		
		System.out.println(String.format("%s = %s", romanNumber, context.getOutput()));
	}
}
  • 程序输出:

MMMCDLXXVIII = 3478

五、适用场合

  • 当有一个语言需要解释执行,并且可以将该语言中的句子表示为一个抽象的语法树时