动机(Motivation)
在软件构建中,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的变成方式来实现将面临非常频繁的变化。
在这种情况下,将特定领域的问题表达为某种语法规则情况下,然后构建一个解释器(解释器并不一样)来解释这样的句子,从而达到解决问题的目的。
意图(Intent)
给定一个语言,定义它的文法的一种表示,并定义一种解释器,这个解释器使用该表示来解释语言中的句子。
---《设计模式》GoF
结构(Structure)
代码
namespace
Interpreter
{
public class MainApp
{
static void Main()
{
string romman = " 六千四百五十二 " ; // 转换成6452,但是怎么解决无穷的位数?
}
}
public class Context // 处理上下文类
{
public Context( string xx)
{
Statement = xx;
}
public string Statement;
public int Data;
}
public abstract class Expression
{
protected Dictionary < string , int > table = new Dictionary < string , int > ( 9 );
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.Lenth == 0 )
return ;
foreach ( string key in table.Keys)
{
int value = table[key];
if (context.Statement.EndsWith(key + GetPostfix())) // 如果是以此关键字结尾比如400=四百
{
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 - this .GetLength());
}
}
}
public abstract string GetPostfix();
public abstract int Multiplier();
public virtual int GetLength()
{
return this .GetPostfix().Length + 1 ;
}
}
// 个
public class GeExpression : Expression
{
public override string GetPostfix()
{
return "" ;
}
public override int GetLength()
{
return 1 ;
}
}
// 十, 百,千处理方法同十
public class ShiExpression : Expression
{
public override string GetPostfix()
{
return " 十 " ;
}
public override int Multiplier()
{
return 10 ;
}
}
public class BaiExpression : Expression{}
public class QianExpression : Expression{}
// 万,亿等等处理类似
public class WanExpression : Expression
{
public override string GetPostfix()
{
return " 万 " ;
}
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 WanExpression());
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 - this .GetLength());
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
context.Data = temp + this .Multiplier() * context.Data;
}
}
}
public override int Multiplier()
{
return 10000 ;
}
}
}
{
public class MainApp
{
static void Main()
{
string romman = " 六千四百五十二 " ; // 转换成6452,但是怎么解决无穷的位数?
}
}
public class Context // 处理上下文类
{
public Context( string xx)
{
Statement = xx;
}
public string Statement;
public int Data;
}
public abstract class Expression
{
protected Dictionary < string , int > table = new Dictionary < string , int > ( 9 );
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.Lenth == 0 )
return ;
foreach ( string key in table.Keys)
{
int value = table[key];
if (context.Statement.EndsWith(key + GetPostfix())) // 如果是以此关键字结尾比如400=四百
{
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 - this .GetLength());
}
}
}
public abstract string GetPostfix();
public abstract int Multiplier();
public virtual int GetLength()
{
return this .GetPostfix().Length + 1 ;
}
}
// 个
public class GeExpression : Expression
{
public override string GetPostfix()
{
return "" ;
}
public override int GetLength()
{
return 1 ;
}
}
// 十, 百,千处理方法同十
public class ShiExpression : Expression
{
public override string GetPostfix()
{
return " 十 " ;
}
public override int Multiplier()
{
return 10 ;
}
}
public class BaiExpression : Expression{}
public class QianExpression : Expression{}
// 万,亿等等处理类似
public class WanExpression : Expression
{
public override string GetPostfix()
{
return " 万 " ;
}
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 WanExpression());
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 - this .GetLength());
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
context.Data = temp + this .Multiplier() * context.Data;
}
}
}
public override int Multiplier()
{
return 10000 ;
}
}
}
Interpreter模式的几个要点
·Interpreter模式的应用场合是Interpreter模式应用中的难点,只有满足"业务规则频繁变化,且类似的模式不断重复出现,并且容易抽象为语法规则的问题"才适合使用Interpreter模式。
·使用Interpreter模式来表示文法规则,从而可以使用面向对象技巧来方便地扩展文法。
·Interpreter模式比较适合简单的文法表示,对于复杂的文法表示,Interpreter模式会产生比较大的类层次结构,需要求助于语法分析生成器这样的标准工具。