Unity实现设计模式——解释器模式

Unity实现设计模式------解释器模式

解释器模式(Interpreter Pattern)是一种按照规定语法进行解析的模式,现实项目中用得较少。 给定一门语言,定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。

下面用一个例子演示:将罗马文字转换为十进制的格式

1.Context

csharp 复制代码
    class Context
    {
        private string _input;
        private int _output;

        // Constructor
        public Context(string input)
        {
            this._input = input;
        }

        // Gets or sets input
        public string Input
        {
            get { return _input; }
            set { _input = value; }
        }

        // Gets or sets output
        public int Output
        {
            get { return _output; }
            set { _output = value; }
        }
    }

2.Expression

解释器基类

csharp 复制代码
    abstract class Expression
    {
        //"MCMXXVIII";
        public void Interpret(Context context)
        {
            if (context.Input.Length == 0)
                return;

            if (context.Input.StartsWith(Nine()))
            {
                context.Output += (9 * Multiplier());
                context.Input = context.Input.Substring(2);
            }
            else if (context.Input.StartsWith(Four()))
            {
                context.Output += (4 * Multiplier());
                context.Input = context.Input.Substring(2);
            }
            else if (context.Input.StartsWith(Five()))
            {
                context.Output += (5 * Multiplier());
                context.Input = context.Input.Substring(1);
            }

            while (context.Input.StartsWith(One()))
            {
                context.Output += (1 * Multiplier());
                context.Input = context.Input.Substring(1);
            }
        }

        public abstract string One();
        public abstract string Four();
        public abstract string Five();
        public abstract string Nine();
        public abstract int Multiplier();
    }

3.ThousandExpression

千位数字解释器

csharp 复制代码
    class ThousandExpression : Expression
    {
        public override string One() { return "M"; }
        public override string Four() { return " "; }
        public override string Five() { return " "; }
        public override string Nine() { return " "; }
        public override int Multiplier() { return 1000; }
    }

4.HundredExpression

百位数字解释器

csharp 复制代码
    class HundredExpression : Expression
    {
        public override string One() { return "C"; }
        public override string Four() { return "CD"; }
        public override string Five() { return "D"; }
        public override string Nine() { return "CM"; }
        public override int Multiplier() { return 100; }
    }

5.TenExpression

十位数字解释器

csharp 复制代码
    class TenExpression : Expression
    {
        public override string One() { return "X"; }
        public override string Four() { return "XL"; }
        public override string Five() { return "L"; }
        public override string Nine() { return "XC"; }
        public override int Multiplier() { return 10; }
    }

6.OneExpression

个位数字解释器

csharp 复制代码
    class OneExpression : Expression
    {
        public override string One() { return "I"; }
        public override string Four() { return "IV"; }
        public override string Five() { return "V"; }
        public override string Nine() { return "IX"; }
        public override int Multiplier() { return 1; }
    }

7.测试

csharp 复制代码
    public class InterpreterExample1 : MonoBehaviour
    {
        void Start()
        {
            string roman = "MCMXXVIII";
            Context context = new Context(roman);

            // Build the 'parse tree'
            List<Expression> tree = new List<Expression>();
            tree.Add(new ThousandExpression());
            tree.Add(new HundredExpression());
            tree.Add(new TenExpression());
            tree.Add(new OneExpression());

            // Interpret
            foreach (Expression exp in tree)
            {
                exp.Interpret(context);
            }

            Debug.Log(roman+" = "+ context.Output);
        }
    }
相关推荐
十五年专注C++开发6 分钟前
面试题:C++11在C++98基础上增加了哪些内容?
开发语言·c++·设计模式·面试·stl·适配器模式
都叫我大帅哥1 小时前
代码界的「跨界婚姻」:桥接模式的鹊桥艺术
java·后端·设计模式
独爱竹子的功夫熊猫2 小时前
从复杂到优雅:用建造者和责任链重塑代码架构
java·后端·设计模式
找了一圈尾巴12 小时前
设计模式(结构型)-桥接模式
设计模式·桥接模式
匹马夕阳12 小时前
java开发中的设计模式之单例模式
java·单例模式·设计模式
啊QQQQQ16 小时前
设计模式-原型模式
java·设计模式·原型模式
xiaowu08017 小时前
C#设计模式-状态模式
设计模式·c#·状态模式
编程侦探17 小时前
【设计模式】适配器模式:让不兼容的接口和谐共处
开发语言·c++·设计模式·适配器模式
骊山道童18 小时前
设计模式-桥接模式
设计模式·桥接模式
程序员JerrySUN18 小时前
设计模式每日硬核训练 Day 12:装饰器模式(Decorator Pattern)完整讲解与实战应用
设计模式·装饰器模式