zoukankan      html  css  js  c++  java
  • 设计模式(十五):解释器模式

    一、定义

    在设定环境中,定义一种规则或者语法,通过解释器来解释规则或者语法的含义.

    二、实例:将  二十一    —>    21

    2.1 设定我们的环境 Context

     public class Context
        {
            public string Input { get; set; }
            public int Output { get; set; }
        }

    2.2 根据语法来解释

    抽象解释器:

     public abstract class Interpreter
        {
            public string FlagStr { get; set; }
            public int Num { get; set; }
            public abstract int Interpret(Context context);
        }

    具体解释:

    二=2

      public class Two : Interpreter
        {
            public Two()
            {
                FlagStr = "";
                Num = 2;
            }
    
            public override int Interpret(Context context)
            {
                return Num;
            }
        }

    一=1

     public class One : Interpreter
        {
            public One()
            {
                FlagStr = "";
                Num = 1;
            }
    
            public override int Interpret(Context context)
            {
                return Num;
            }
        }

    十=*10

     public class Tenfold : Interpreter
        {
            public Tenfold()
            {
                FlagStr = "";
                Num = 10;
            }
            public override int Interpret(Context context)
            {
                return context.Output*Num;
            }
        }

    再封装一下:

     public class InterpretPrivoder
        {
            public int FormatStr(Context context)
            {
                foreach (char c in context.Input)
                {
                    switch (c)
                    {
                        case '': context.Output += new One().Interpret(context); break;
                        case '': context.Output += new Two().Interpret(context); break;
                        case '': context.Output = new Tenfold().Interpret(context); break;
                    }
                }
                return context.Output;
            }
        }

    其中,未结束符为二和一,结束符为十

    客户端:

     //------------------------解释器模式-----------------------
                Interpreter.Context interpretContext = new Interpreter.Context();
                interpretContext.Input = "二十一";
                Interpreter.InterpretPrivoder interpreter = new Interpreter.InterpretPrivoder();
                interpreter.FormatStr(interpretContext);
                Console.WriteLine(interpretContext.Output);
                Console.ReadKey();

    结果:

    三、总结

    解释器模式,实在一种模式经常出现,并不断变化。我们可以使用解释器。

    缺点就是容易子类膨胀

  • 相关阅读:
    APIO2015雅加达的摩天楼
    索引(填坑进度:0.1‰)
    树形 DP 笔记 · 一
    「已弃坑」DP 优化的各种姿势 (From CF)
    C++ 的位运算:__builtin, bitset
    LA 7158. ACM-ICPC World Finals 2015 I. Ship Traffic
    LA 7155. ACM-ICPC World Finals 2015 F. Keyboarding
    LA 7150. ACM-ICPC World Finals 2015 A. Amalgamated Artichokes
    LA 7587. ACM-ICPC World Finals 2016 L. Swap Space
    LA 7578. ACM-ICPC World Finals 2016 C. Ceiling Function
  • 原文地址:https://www.cnblogs.com/sunchong/p/5133911.html
Copyright © 2011-2022 走看看