日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發布時間:2025/3/18 asp.net 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式学习笔记--解释器模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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

總結

以上是生活随笔為你收集整理的设计模式学习笔记--解释器模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。