解譯器模式-Interpreter Pattern

設計模式系列

目的:定義一個語言其文法,使用一個解譯器來表示這個語言的描述。

應用場景:Interpreter模式的應用場合是interpreter模式應用中的難點,只有滿足「業務規則頻繁變化,且類似的模式不斷重複出現,並且容易抽象為語法規則的問題」才適合使用Interpreter模式。
 

優點:

  • 易於改變和擴展文法。由於在解釋器模式中使用類來表示語言的文法規則,因此可以通過繼承等機制來改變或擴展文法。
  • 每一條文法規則都可以表示為一個類,因此可以方便地實現一個簡單的語言。
  • 實現文法較為容易。在抽象語法樹中每一個表達式節點類的實現方式都是相似的,這些類的代碼編寫都不會特別複雜,還可以通過一些工具自動生成節點類代碼。
  • 增加新的解釋表達式較為方便。如果用戶需要增加新的解釋表達式只需要對應增加一個新的終結符表達式或非終結符表達式類,原有表達式類代碼無須修改,符合「開閉原則」。


缺點:

  • 對於複雜文法難以維護。在解釋器模式中,每一條規則至少需要定義一個類,因此如果一個語言包含太多文法規則,類的個數將會急劇增加,導致系統難以管理和維護,此時可以考慮使用語法分析程序等方式來取代解釋器模式。
  • 執行效率較低。由於在解釋器模式中使用了大量的循環和遞歸調用,因此在解釋較為複雜的句子時其速度很慢,而且代碼的調試過程也比較麻煩。


 

    class Program
    {
        static void Main(string[] args)
        {
            Context context = new Context();
            IList<AbstractExpression> list = new List<AbstractExpression>();
            list.Add(new TerminalExpression());
            list.Add(new NonterminalExpression());
            list.Add(new TerminalExpression());
            list.Add(new TerminalExpression());

            foreach (var exp in list)
            {
                exp.Interpret(context);
            }
        }
    }

    class Context
    {
        private string input;
        private string output;

        public string Input {
            get {
                return input;
            } set {
                input = value;
            }
        }

        public string Output
        {
            get
            {
                return output;
            }
            set
            {
                output = value;
            }
        }
    }


    abstract class AbstractExpression
    {
        public abstract void Interpret(Context context);
    }

    class TerminalExpression : AbstractExpression
    {
        public override void Interpret(Context context)
        {
            Console.WriteLine("終端解譯器");
        }

    }
    class NonterminalExpression : AbstractExpression
    {
        public override void Interpret(Context context)
        {
            Console.WriteLine("非終端解譯器");
        }

    }

老E隨手寫