标签:
动机(Motivation)
件构建过程中,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变化。
种情况下,特定的领域的问题表达式为某种语法规则下的句子,然后构建一个解释器来解释这样的句子,从而达到解决问题的目的。
意图(Intent)
给定一个语言,定义它的文法的一种表示,并定义一种解释器,这个解释器使用该表示来解释语言中的句子。
interpreter 模式的几个要点
类图
Code
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DesignPatterns { class Program { static void Main(string[] args) { string roman = "三百六十万六千四百五十二";//6452 Context context = new Context(roman); ArrayList tree = new ArrayList(); tree.Add(new GeExpression()); tree.Add(new ShiExpression()); tree.Add(new BaiExpression()); tree.Add(new QiaExpression()); tree.Add(new WanExpression()); foreach (Expresion exp in tree) { exp.Interpret(context); } Console.WriteLine("{0}=={1}",roman,context.Data); Console.ReadLine(); } } 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 Expresion { protected Dictionary<string, int> table = new Dictionary<string, int>(9); public Expresion() { 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 * this.Multiplier(); context.Statement = context.Statement.Substring(0,context.Statement.Length-this.GetLength()); } 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 + 1; } } public class WanExpression : Expresion { public override string GetPostfix() { return "万"; } public override int Multiplier() { return 10000; } public override int GetLength() { return 1; } public override void Interpret(Context context) { if (context.Statement.Length == 0) return; ArrayList tree = new ArrayList(); tree.Add(new GeExpression()); tree.Add(new ShiExpression()); tree.Add(new BaiExpression()); tree.Add(new QiaExpression()); 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-this.GetLength()); foreach (Expresion exp in tree) { exp.Interpret(context); } context.Data = temp + this.Multiplier() * context.Data; } } } } public class QiaExpression : Expresion { public override string GetPostfix() { return "千"; } public override int Multiplier() { return 1000; } } public class BaiExpression : Expresion { public override string GetPostfix() { return "百"; } public override int Multiplier() { return 100; } } public class ShiExpression : Expresion { public override string GetPostfix() { return "十"; } public override int Multiplier() { return 10; } } public class GeExpression : Expresion { public override string GetPostfix() { return ""; } public override int Multiplier() { return 1; } public override int GetLength() { return 1; } } }
标签:
原文地址:http://www.cnblogs.com/vicxi/p/4857465.html