解释器模式:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
如果一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表述为一个简单语言中的句子。这样就可以构建一个解释器,该解释器通过解释这些句子来解决该问题。正则表达式就是一种应用。
/**
* 包含解释器之外的一些全局信息
*
* @author administrator
*
*/
public class Context {
private String input;
private String output;
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String getOutput() {
return output;
}
public void setOutput(String output) {
this.output = output;
}
}
/**
* 声明一个抽象的解释操作,这个接口为抽象语法树中所有的节点所共享
*
* @author administrator
*
*/
public abstract class AbstractExpression {
public abstract void interpret(Context context);
}
/**
* 非终结符表达式,为文法中的非终结符实现解释操作。对文法中每一条规则R1、R2 ... ... Rn都需要一个具体的非终结符表达式类。
*
* @author administrator
*
*/
public class NonTerminalExpression extends AbstractExpression {
@Override
public void interpret(Context context) {
System.out.println("非终端解释器");
}
}
/**
* 实现与文法中的终结符相关联的解释操作,文法中每一个终结符都有一个具体终结表达式与之相对应
*
* @author administrator
*
*/
public class TerminalExpression extends AbstractExpression {
@Override
public void interpret(Context context) {
System.out.println("终端解释器");
}
}
/**
* 构建表示该文法定义的语言中一个特定的句子的抽象语法树,调用解释操作
*
* @author administrator
*
*/
public class InterpreterClient {
public static void main(String[] args) {
Context context = new Context();
List<AbstractExpression> list = new ArrayList<AbstractExpression>();
list.add(new TerminalExpression());
list.add(new NonTerminalExpression());
list.add(new TerminalExpression());
list.add(new TerminalExpression());
for (AbstractExpression expression : list) {
expression.interpret(context);
}
}
}
案例
/**
* 解释器模式(Interpreter)
* 演奏内容类
*/
public class PlayContext {
// 演奏文本
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
/**
* 解释器模式(Interpreter)
* 表达式类
*/
public abstract class Expression {
// 解释器
public void interpret(PlayContext context) {
if (context.getText().length() == 0) {
return;
} else {
String playKey = context.getText().substring(0, 1);
context.setText(context.getText().substring(2));
double playValue = Double.parseDouble(context.getText().substring(0, context.getText().indexOf(" ")));
context.setText(context.getText().substring(context.getText().indexOf(" ") + 1));
excute(playKey, playValue);
}
}
// 执行
public abstract void excute(String key, double value);
}
/**
* 解释器模式(Interpreter)
* 音符类
*/
public class Note extends Expression {
@Override
public void excute(String key, double value) {
String note = "";
switch (key) {
case "C":
note = "1";
break;
case "D":
note = "2";
break;
case "E":
note = "3";
break;
case "F":
note = "4";
break;
case "G":
note = "5";
break;
case "A":
note = "6";
break;
case "B":
note = "7";
break;
}
System.out.print(note + " ");
}
}
/**
* 解释器模式(Interpreter)
* 音阶类
*/
public class Scale extends Expression {
@Override
public void excute(String key, double value) {
String scale = "";
switch ((int) value) {
case 1:
scale = "低音";
break;
case 2:
scale = "中音";
break;
case 3:
scale = "高音";
break;
default:
throw new RuntimeException("Exception");
}
System.out.print(scale + " ");
}
}
/**
* 解释器模式(Interpreter)
* 音速类
*/
public class Speed extends Expression {
@Override
public void excute(String key, double value) {
String speed = "";
if (value < 500) {
speed = "快速";
} else if (value >= 1000) {
speed = "慢速";
} else {
speed = "中速";
}
System.out.print(speed + " ");
}
}
/**
* 解释器模式(Interpreter)
* 客户端main方法
*/
public class Client {
public static void main(String[] args) {
PlayContext context = new PlayContext();
// 音乐-上海滩
System.out.println("上海滩:");
context.setText("T 500 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E " +
"0.5 D 3 ");
Expression expression = null;
try {
while (context.getText().length() > 0) {
String str = context.getText().substring(0, 1);
switch (str) {
case "O":
expression = new Scale();
break;
case "T":
expression = new Speed();
break;
case "C":
case "D":
case "E":
case "F":
case "G":
case "A":
case "B":
case "P":
expression = new Note();
break;
default:
throw new RuntimeException("Exception");
}
expression.interpret(context);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}