设计模式之—解释器模式<InterpreterPattern>

解释器模式:给定一个语言,定义他的文法的一种表示,并定义一个解释器,这个解释器用于解释语言中的句子。比如文件解密

按如下规则编写程序:

演奏内容类(Context)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterpreterPattern.CLASS
{
    //需要解释的文本
    class Context
    {
        private string text;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }
    }
}
View Code

解释器类(AbstractExpression)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterpreterPattern.CLASS
{
    //解释器
    abstract class AbstractExpression
    {
        public void Interpreter(Context context)
        {
            if (context.Text.Length == 0)
            {
                return;
            }
            else
            {
                string key = context.Text.Substring(0, 1);
                context.Text = context.Text.Substring(2);
                //indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
                double value = Convert.ToDouble(context.Text.Substring(0, context.Text.IndexOf(" ")));
                context.Text = context.Text.Substring(context.Text.IndexOf(" ") + 1);

                Excute(key, value);
            }
        }

        public abstract void Excute(string key, double value);
    }
}
View Code

终结符表达式类(TerminalExpression)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterpreterPattern.CLASS
{
    //音节类
    class TerminalExpreesion : AbstractExpression
    {
        public override void Excute(string key, double value)
        {
            string note = "";
            switch (key)
            {
                case "C":
                    note = "1";
                    break;
                case "D":
                    note = "2";
                    break;
                case "E":
                    note = "3";
                    break;
                case "F":
                    note = "4";
                    break;
                case "G":
                    note = "5";
                    break;
                case "A":
                    note = "6";
                    break;
                case "B":
                    note = "7";
                    break;
                default :
                    break;
            }
            Console.Write("{0} ", note);
        }
    }
}
View Code

终结符表达式1类(TerminalExpression)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
namespace InterpreterPattern.CLASS
{
    //音符类
    class TerminalExpression1:AbstractExpression
    {
        public override void Excute(string key, double value)
        {
            string scale = "";
            switch (Convert.ToInt32(value))
            {
                case 1:
                    scale = "低音";
                    break;
                case 2:
                    scale = "中音";
                    break;
                case 3:
                    scale = "高音";
                    break;
                default:
                    break;
            }
            Console.Write("{0} ", scale);
        }
    }
}
View Code

测试类(TestMain)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using InterpreterPattern.CLASS;
namespace InterpreterPattern
{
    class TeatMain
    {
        static void Main(string[] args)
        {
            Context context = new Context();
            Console.WriteLine("上海滩:");
            context.Text = "O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 ";
            AbstractExpression expression = null;
            try
            {
                while (context.Text.Length > 0)
                {
                    string str = context.Text.Substring(0, 1);
                    switch (str)
                    {
                        case "O":
                            expression = new TerminalExpression1();
                            break;
                        case "C":
                        case "D":
                        case "E":
                        case "F":
                        case "G":
                        case "A":
                        case "B":
                        case "P":
                            expression = new TerminalExpreesion();
                            break;
                    }
                    expression.Interpreter(context);
                }
            }
            catch(Exception e) 
            {
                Console.WriteLine(e.Message);
            }

            Console.Read();
        }
    }
}
View Code

测试结果:

要么忍,要么狠,要么滚!
原文地址:https://www.cnblogs.com/zxd543/p/3307579.html