解释器模式:给定一种语言,定义它的文法表示,并定义一个解释器,这个解释器用来解释语言中的句子

    优点:当一个语言需要执行,并且你可将该语言中的句子表示为抽象语法树时,可使用解释器模式

    实现:做一个音乐解释器,O代表音阶,ABCDEFG代表音符
    1、演奏内容类

    1. /**
    2. * 演奏内容类
    3. */
    4. public class PlayContext {
    5. private String text;
    6. public String getText() {
    7. return text;
    8. }
    9. public void setText(String text) {
    10. this.text = text;
    11. }
    12. }

    2、表达式类

    1. /**
    2. * 抽象表达式类
    3. */
    4. public abstract class Expression {
    5. /**
    6. * 解释器
    7. */
    8. public void interpret(PlayContext context){
    9. if (context.getText() == null || context.getText().length() == 0){
    10. return;
    11. }else{
    12. String palyKey = context.getText().substring(0 , 1);
    13. context.setText(context.getText().substring(2));
    14. Double palyValue = Double.valueOf(context.getText().substring(0 , context.getText().indexOf(" ")));
    15. context.setText(context.getText().substring(context.getText().indexOf(" ") + 1));
    16. excute(palyKey , palyValue);
    17. }
    18. }
    19. /**
    20. * 执行
    21. */
    22. public abstract void excute(String key , Double value);
    23. }
    24. /**
    25. * 音符类
    26. */
    27. public class NoteExpression extends Expression{
    28. public void excute(String key , Double value){
    29. String note = "";
    30. switch (key){
    31. case "C":
    32. note = "1";
    33. break;
    34. case "D":
    35. note = "2";
    36. break;
    37. case "E":
    38. note = "3";
    39. break;
    40. case "F":
    41. note = "4";
    42. break;
    43. case "G":
    44. note = "5";
    45. break;
    46. case "A":
    47. note = "6";
    48. break;
    49. case "B":
    50. note = "7";
    51. break;
    52. }
    53. System.out.print(MessageFormat.format("{0} ", note));
    54. }
    55. }
    56. /**
    57. * 音节类
    58. */
    59. public class ScaleExpression extends Expression{
    60. public void excute(String key , Double value){
    61. String scale = "";
    62. switch (value.intValue()){
    63. case 1:
    64. scale = "低音";
    65. break;
    66. case 2:
    67. scale = "中音";
    68. break;
    69. case 3:
    70. scale = "高音";
    71. break;
    72. }
    73. System.out.print(MessageFormat.format("{0} ", scale));
    74. }
    75. }

    3、测试类

    1. /**
    2. * 测试解释器模式
    3. */
    4. public class TestExpression {
    5. public static void main(String [] args){
    6. PlayContext context = new PlayContext();
    7. context.setText("O 2 E 0.5 G 0.5 A 3 O 1 B 2 ");
    8. Expression expression = null;
    9. while (context.getText().length() > 0){
    10. String str = context.getText().substring(0 , 1);
    11. switch (str){
    12. case "O":
    13. expression = new ScaleExpression();
    14. break;
    15. case "C":
    16. case "D":
    17. case "E":
    18. case "F":
    19. case "G":
    20. case "A":
    21. case "B":
    22. case "P":
    23. expression = new NoteExpression();
    24. break;
    25. }
    26. expression.interpret(context);
    27. }
    28. }
    29. }

    测试结果:
    image.png

    如果一个特定类型问题的发生频率足够高,那么就值得将该问题的各个实例表述为一个简单的语言中的句子,这样就可以构建一个解释器,通过解释器解释这些句子来解决问题