码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式学习笔记--解释器模式

时间:2016-06-03 21:24:50      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 1 using System;
 2 
 3 namespace Interpreter
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/3 19:42:24 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Context说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class Context
12     {
13         private string text;
14 
15         public string PlayText
16         {
17             get { return text; }
18             set { text = value; }
19         }
20     }
21 }
View Code
技术分享
 1 using System;
 2 
 3 namespace Interpreter
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/3 19:41:33 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// AbstractExpression说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public abstract class AbstractExpression
12     {
13         public void Interpret(Context context)
14         {
15             if (context.PlayText.Length == 0)
16             {
17                 return; 
18             }
19             else
20             {
21                 string playKey = context.PlayText.Substring(0,1);
22                 context.PlayText = context.PlayText.Substring(2);
23                 double playValue = Convert.ToDouble(context.PlayText.Substring(0,context.PlayText.IndexOf(" ")));
24                 context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ")+1);
25 
26                 Execute(playKey,playValue);
27             }
28         }
29 
30         public abstract void Execute(string key,double value);
31     }
32 }
View Code
技术分享
 1 using System;
 2 
 3 namespace Interpreter
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/3 19:59:36 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Note说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class Note : AbstractExpression
12     {
13         public override void Execute(string key, double value)
14         {
15             string note = "";
16             switch (key)
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             Console.WriteLine("{0}", note);
41         }
42     }
43 }
View Code
技术分享
 1 using System;
 2 
 3 namespace Interpreter
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/3 20:02:48 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Scale说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class Scale : AbstractExpression
12     {
13         public override void Execute(string key, double value)
14         {
15             string scale = "";
16 
17             switch (Convert.ToInt32(value))
18             {
19                 case 1:
20                     scale = "低音";
21                     break;
22                 case 2:
23                     scale = "中音";
24                     break;
25                 case 3:
26                     scale = "高音";
27                     break;
28             }
29 
30             Console.WriteLine("{0}", scale);
31         }
32     }
33 }
View Code
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 
 4 namespace Interpreter
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Context context = new Context();
11             context.PlayText = "O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5";
12 
13             AbstractExpression exp = null;
14 
15             try
16             {
17                 while (context.PlayText.Length > 0)
18                 {
19                     string str = context.PlayText.Substring(0, 1);
20                     switch (str)
21                     {
22                         case "O":
23                             exp = new Scale();
24                             break;
25                         case "C":
26                         case "D":
27                         case "E":
28                         case "F":
29                         case "A":
30                         case "B":
31                         case "P":
32                             exp = new Note();
33                             break;
34                     }
35                     exp.Interpret(context);
36                 }
37             }
38             catch (Exception ex)
39             {
40                 Console.WriteLine(ex.Message);
41             }
42         }
43     }
44 }
View Code

 

设计模式学习笔记--解释器模式

标签:

原文地址:http://www.cnblogs.com/bzyzhang/p/5557487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!