[設計模式練習]解譯器模式

[設計模式練習]解譯器模式

直接進入主題

將阿拉伯數字金額轉中文金額(不含角跟分)

零的部分還要再加強處理=.=

類別圖

Interpreter

物件檔定義

using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace DesignModelTest.解譯器
{
    /// <summary>
    /// context類別 : 輸入文字
    /// </summary>
    class WordContext
    {
        private string text;
        public string expressiontext
        {
            get { return text; }
            set { text = value; }
        }
    }
    /// <summary>
    /// AbstractExpression類別:表示式
    /// </summary>
    abstract class Expression
    {
        /// <summary>
        /// 執行解譯動作.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Interpret(WordContext context)
        {
            if (context.expressiontext.Length == 0)
            {
                
                return;
            }
            else
            {
                //每切一個字就轉譯出來
                string wordkey = context.expressiontext.Substring(0, 1);
                int wordlength = context.expressiontext.Length;
                context.expressiontext = context.expressiontext.Substring(1);                
                Execute(wordkey,wordlength/4,wordlength%4);
            }

        }
        public abstract void Execute(string key,int divisor,int remainder);
    }
    /// <summary>
    /// ConcreteExpression類別:阿拉伯數字金額轉中文數字金額(非零數字)
    /// </summary>
    class NumToChi : Expression
    {
        public override void Execute(string key,int divisor,int remainder)
        {
            string note = string.Empty;
            switch (key.ToUpper())
            {
                case "0": note = string.Empty;
                    break;
                case "1": note = "壹";
                    break;
                case "2": note = "貳";
                    break;
                case "3": note = "參";
                    break;
                case "4": note = "肆";
                    break;
                case "5": note = "伍";
                    break;
                case "6": note = "陸";
                    break;
                case "7": note = "柒";
                    break;
                case "8": note = "捌";
                    break;
                case "9": note = "玖";
                    break;
                default: break;                    
            }
            switch (remainder)
            {
                case 0:                    
                    note += "千";
                    break;
                case 1:                    
                    break;
                case 2:                    
                    note += "拾";
                    break;
                case 3:
                    note += "佰";                    
                    break;
                default:
                    break;
            }
            switch (divisor)
            {
                case 0:
                    break;
                case 1:
                    if (remainder == 1)
                    {
                        note += "萬";
                    }
                    break;
                case 2:
                    if (remainder == 1)
                    {
                        note += "億";
                    }
                    break;
                case 3:
                    if (remainder == 1)
                    {
                        note += "兆";
                    }
                    break;
                default:
                    break;
            }
            Console.Write("{0}",note);
            if(remainder == 1 && divisor == 0)
                Console.Write("元整");
        }
    }

    /// <summary>
    /// ConcreteExpression類別:阿拉伯數字金額轉中文數字金額(零數字)
    /// </summary>
    class NumZeroToChi : Expression
    {
        public override void Execute(string key, int divisor, int remainder)
        {
            string note = string.Empty;
            
            if(key != "0")
                return;
            if (remainder != 1)
            {
                note = string.Empty;
            }
            else
            {
                switch (divisor)
                {
                    case 0:                             
                        break;
                    case 1:                        
                            note = "萬";                        
                        break;
                    case 2:                        
                            note = "億";                        
                        break;
                    case 3:                        
                            note = "兆";                        
                        break;
                    default:
                        break;
                }
            }
            Console.Write("{0}", note);
            if (remainder == 1 && divisor == 0)
                Console.Write("零元整");
        }
    }
    /// <summary>
    /// 利用反射來產生expression物件
    /// </summary>
    class ExpressionFacotry
    {
        //組件名稱
        private static readonly string AssemblyName = "DesignModelTest";        
        public DesignModelTest.解譯器.Expression GetExpression(string num)
        {
            DesignModelTest.解譯器.Expression expression = null;
            string className = AssemblyName + ".解譯器.";                        
            switch (num)
            {
                        case "0":
                            className += "NumZeroToChi";
                            expression = (NumZeroToChi)Assembly.Load(AssemblyName).CreateInstance(className);
                            break;
                        case "1": 
                        case "2": 
                        case "3": 
                        case "4": 
                        case "5": 
                        case "6": 
                        case "7": 
                        case "8":
                        case "9": 
                            className += "NumToChi";
                            expression = (NumToChi)Assembly.Load(AssemblyName).CreateInstance(className);
                            break;
                        default: break;
            }
            return expression;
        }
    }
      
}

用戶端程式

            //要解譯的文字
            DesignModelTest.解譯器.WordContext wordcontext = new DesignModelTest.解譯器.WordContext();
            
            wordcontext.expressiontext = "33344";
            Console.WriteLine("翻譯前:{0}", wordcontext.expressiontext);
            try
            {
                Console.Write("翻譯後:");
                //解譯物件
                DesignModelTest.解譯器.Expression ex = null;
                //管理解譯的物件
                DesignModelTest.解譯器.ExpressionFacotry expfactory = new DesignModelTest.解譯器.ExpressionFacotry();
                //進行解譯
                while (wordcontext.expressiontext.Length > 0)
                {                    
                    string num = wordcontext.expressiontext.Substring(0, 1);                    
                    //利用反射取得物件
                    ex = expfactory.GetExpression(num);               
                    ex.Interpret(wordcontext);
                }
                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.Read();
            }
            #endregion

輸出結果

pic2