设为首页 加入收藏

TOP

【设计模式】15.解释器模式(一)
2019-09-03 02:11:31 】 浏览:28
Tags:设计模式 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.substrin
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇设计模式六大原则(3):依赖倒置.. 下一篇【设计模式】13、责任链模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目