介绍
解释器模式和字面意思一样,作用主要是为了解释某些特别的字符,比如Java语言的,字节码解释成机器码的过程,但今天不可能举这么复杂的例子,那么我参考了大话设计模式中的例子,并简化了其中某些我认为不必要的“语法”。
例子如下:
- 我希望将“CDEFGAB”替代“1234567”的音符(也就是Do-Re-Mi-Fa-So-La-Ti)
- 将“1 2 3” 替代 “低音 中音 高音”
于是替代后的一串字符如下:2 E G A E G D E G A 3 C 2 A G C E D(输入)
那么原来的字符为:中音 3 5 6 3 5 2 3 5 6 高音 1 中音 6 5 1 3 2 (输出)
现在我希望将替代后的字符输入程序,程序能打印原来的字符
还是老样子,先看UML图
UML图
代码
- PlayContext:是用来存储和管理需要解释的内容的
public class PlayContext {
private String text;
private int index;
public PlayContext(String text) {
this.text = text;
index = 0;
}
public char getValue() {
return text.charAt(index);
}
public void next() {
index = index + 2;
}
public boolean hasNext(){
return text.length() > index;
}
}
- 解释器,这里值模拟了音符和音阶的解释,如果想增加比如“速度”的解释,增加子类即可
public abstract class Expression {
public abstract void interpret(char value);
}
/**
* 音符类
*/
class Note extends Expression {
@Override
public void interpret(char value) {
String note = null;
switch (value){
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 + " ");
}
}
/**
* 音阶类
*/
class Scale extends Expression {
@Override
public void interpret(char value) {
String scale = null;
switch (value){
case '1':scale="低音";break;
case '2':scale="中音";break;
case '3':scale="高音";break;
}
System.out.print(scale + " ");
}
}
- 调用方式和打印结果
public static void main(String[] args) {
String str = "2 E G A E G D E G A 3 C 2 A G C E D";
PlayContext context = new PlayContext(str);
Expression expression = null;
while (context.hasNext()){
switch (context.getValue()){
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
expression = new Note();break;
case '1':
case '2':
case '3':
expression = new Scale();break;
}
expression.interpret(context.getValue());
context.next();
}
}
中音 3 5 6 3 5 2 3 5 6 高音 1 中音 6 5 1 3 2
总结
- 很现显然,调用方法(main)还有很多可以优化的地方,但这里是主要说解释器模式,也就不深入了