【设计模式】15.解释器模式

 1 package com.shejimoshi.behavioral.Interpreter;
 2 
 3 
 4 /**
 5  * 功能:演奏文本
 6  * 时间:2016年3月3日上午9:26:19
 7  * 作者:cutter_point
 8  */
 9 public class PlayContext
10 {
11     private String text;
12     
13     public PlayContext()
14     {
15     }
16     
17     public PlayContext(String text)
18     {
19         this.text = text;
20     }
21 
22     public String getText()
23     {
24         return text;
25     }
26 
27     public void setText(String text)
28     {
29         this.text = text;
30     }
31     
32 }
 1 package com.shejimoshi.behavioral.Interpreter;
 2 
 3 
 4 /**
 5  * 功能:抽象表达式
 6  * 时间:2016年3月3日上午9:28:02
 7  * 作者:cutter_point
 8  */
 9 public abstract class AbstractExpress
10 {
11     //解释器,吧我们的表达式进行分解
12     public void interpret(PlayContext context)
13     {
14         if(context.getText().length() == 0)
15         {
16             return;    //这个表示表达式长度为0,没有需要处理的
17         }
18         else
19         {
20             //这里吧 O 3 E 0.5 G 0.5 A 3 转化为 E 0.5 G 0.5 A 3
21             String playKey = context.getText().substring(0, 1);
22             context.setText(context.getText().substring(2));    //3 E 0.5 G 0.5 A 3
23             String playValue = context.getText().substring(0, context.getText().indexOf(" "));
24             context.setText(context.getText().substring(context.getText().indexOf(" ") + 1));    //E 0.5 G 0.5 A 3
25             
26             //执行解释器
27             excute(playKey, playValue);
28         }
29     }
30     
31     public abstract void excute(String playKey, String playValue);
32 }
 1 package com.shejimoshi.behavioral.Interpreter;
 2 
 3 
 4 /**
 5  * 功能:音符类
 6  * 时间:2016年3月3日上午9:40:33
 7  * 作者:cutter_point
 8  */
 9 public class Note extends AbstractExpress
10 {
11 
12     @Override
13     public void excute(String playKey, String playValue)
14     {
15         String note = "";
16         switch(playKey)
17         {
18         case "C":
19             note = "1";
20             break;
21         case "D":
22             note = "2";
23             break;
24         case "E":
25             note = "3";
26             break;
27         case "F":
28             note = "4";
29             break;
30         case "G":
31             note = "5";
32             break;
33         case "A":
34             note = "6";
35             break;
36         case "B":
37             note = "7";
38             break;
39         }
40         
41         System.out.print(note + " ");
42     }
43 }
 1 package com.shejimoshi.behavioral.Interpreter;
 2 
 3 
 4 /**
 5  * 功能:音符类
 6  * 时间:2016年3月3日上午9:48:49
 7  * 作者:cutter_point
 8  */
 9 public class Scale extends AbstractExpress
10 {
11 
12     @Override
13     public void excute(String playKey, String playValue)
14     {
15         String scale = "";
16         switch(playValue)
17         {
18         case "1":
19             scale = "低音";
20             break;
21         case "2":
22             scale = "中音";
23             break;
24         case "3":
25             scale = "高音";
26             break;
27         }
28         
29         System.out.print(scale + " ");
30     }
31 
32 }
 1 package com.shejimoshi.behavioral.Interpreter;
 2 
 3 
 4 /**
 5  * 功能:解释器模式,给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语句中的句子
 6  * 适用:当有一个有一个简单的语法规则,比如一个sql语句,如果我们需要根据sql语句进行rm转换,就可以使用解释器模式来对语句进行解释。
 7         一些重复发生的问题,比如加减乘除四则运算,但是公式每次都不同,有时是a+b-c*d,有时是a*b+c-d,等等等等个,公式千变万化,
 8         但是都是由加减乘除四个非终结符来连接的,这时我们就可以使用解释器模式。
 9  * 时间:2016年3月3日上午8:43:58
10  * 作者:cutter_point
11  */
12 public class Test
13 {
14     public static void main(String[] args)
15     {
16 //        String str = "tes asd";
17 //        System.out.println(str.substring(2));
18 //        System.out.println(str.substring(1, 3));
19         PlayContext text = new PlayContext();
20         //上海滩
21         System.out.println("上海滩");
22         text.setText("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 ");
23         AbstractExpress expression = null;
24         try
25         {
26             while(text.getText().length() > 0)
27             {
28                 //这里分解字符串
29                 String str = text.getText().substring(0, 1);
30                 switch(str)
31                 {
32                 case "O":
33                     expression = new Scale();
34                     break;
35                 default:
36                     expression = new Note();
37                     break;
38                 }//switch
39                 expression.interpret(text);
40             }
41         } 
42         catch (Exception e)
43         {
44             e.printStackTrace();
45         }
46     }
47 }

测试结果:

上海滩
中音 3 5 6 3 5 2 3 5 6 高音 1 中音 6 5 1 3 2 

  

原文地址:https://www.cnblogs.com/cutter-point/p/5237469.html