zoukankan      html  css  js  c++  java
  • 设计模式学习笔记--解释器模式

     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
  • 相关阅读:
    [Windows Server 2012] SQL Server 备份和还原方法
    [Windows Server 2012] 更改服务器密码
    [Windows Server 2012] 初识Windows Server 2012
    [Windows Server 2012] 更换PHP版本方法
    [Windows Server 2008] IP安全策略限制端口方法
    [Windows Server 2008] Windows防火墙设置
    [Windows Server 2003] 初识Windows Server 2003
    [Windows Server 2003] 手工创建安全网站
    Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想
    Codeforces Round #324 (Div. 2) Marina and Vasya 乱搞推理
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5557487.html
Copyright © 2011-2022 走看看