设计模式学习笔记--解释器模式
生活随笔
收集整理的這篇文章主要介紹了
设计模式学习笔记--解释器模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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
?
轉載于:https://www.cnblogs.com/bzyzhang/p/5557487.html
總結
以上是生活随笔為你收集整理的设计模式学习笔记--解释器模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Linux(ubuntu server
- 下一篇: XML注入介绍--XXE,XEE,xpa