设为首页 加入收藏

TOP

设计模式15:Interpreter 解释器模式(行为型模式)(一)
2019-09-03 03:07:37 】 浏览:32
Tags:设计模式 Interpreter 解释 模式 行为

Interpreter 解释器模式(行为型模式)

 

动机(Motivation)

在软件构建过程中,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变化。

在这种情况下,将特定领域的问题表达为某种语法规则下的句子,然后构建一个解释器来解释这样的句子,从而达到解决问题的目的。

 

意图(Intent)

给定一个语言,定义它的文法的一种表示,并定义一种解释器,这个解释器用来解释语言中的句子。——《设计模式》GoF

 

中文数字转换为阿拉伯数字

    public class Context
    {
        private string statement;
        private int data;

        public Context(string statement)
        {
            this.statement = statement;
        }

        public string Statement
        {
            get
            {
                return statement;
            }
            set
            {
                statement = value;
            }
        }

        public int Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }
    }

    public abstract class Expression
    {
        protected Dictionary<string, int> table = new Dictionary<string, int>();

        public Expression()
        {
            table.Add("", 1);
            table.Add("", 2);
            table.Add("", 3);
            table.Add("", 4);
            table.Add("", 5);
            table.Add("", 6);
            table.Add("", 7);
            table.Add("", 8);
            table.Add("", 9);
        }

        public virtual void Interpret(Context context)
        {
            if (context.Statement.Length == 0)
            {
                return;
            }

            foreach (string key in table.Keys)
            {
                int value = table[key];
                if (context.Statement.EndsWith(key + GetPostfix()))
                {
                    context.Data += value * Multiplier();
                    context.Statement = context.Statement.Substring(0, context.Statement.Length - GetLength() - 1);
                }

                if (context.Statement.EndsWith(""))
                {
                    context.Statement = context.Statement.Substring(0, context.Statement.Length - 1);
                }
            }
        }

        public abstract string GetPostfix();
        public abstract int Multiplier();
        public virtual int GetLength()
        {
            return this.GetPostfix().Length;
        }
    }

    public class GeExpression : Expression
    {
        public override string GetPostfix()
        {
            return string.Empty;
        }
        public override int Multiplier()
        {
            return 1;
        }
        public override int GetLength()
        {
            return 0;
        }
    }
    public class ShiExpression : Expression
    {
        public override string GetPostfix()
        {
            return "";
        }

        public override int Multiplier()
        {
            return 10;
        }
    }
    public class BaiExpression : Expression
    {
        public override string GetPostfix()
        {
            return "";
        }
        public override int Multiplier()
        {
            return 100;
        }
    }
    public class QianExpression : Expression
    {
        public override string GetPostfix()
        {
            return "";
        }
        public override int Multiplier()
        {
            return 1000;
        }
    }
    public class WanExpression : Expression
    {
        public override string GetPostfix()
        {
            return "";
        }
        public override int Multiplier()
        {
            return 10000;
        }
        public override void Interpret(Context context)
        {
            if (context.Statement.Length == 0)
            {
                return;
            }

            List<Expression> tree = new List<Expression>();
            tree.Add(new GeExpression());
            tree.Add(new ShiExpression());
            tree.Add(new BaiExpression());
            tree.Add(new QianExpression());

            foreach (string key in table.Keys)
            {
                if (context.Statement.EndsWith(this.GetPostfix()))
                {
                    int temp = context.Data;
                    context.Data = 0;
                    context.Statement = context.Statement.Substring(0, context.Statement.Length - 1);

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

                    context.Data = temp + this.Multiplier() * context.Data;
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇抽象工厂 下一篇我的处女作——设计模式之状态模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目