极速理解设计模式系列:24.解释器模式(Interpreter Pattern)
五个角色 :场景(Context)、抽象表达式(Component)、终结符表达式(TerminalExpression)、非终结符表达式(NonterminalExpression)、客户端(Client)
场景(Context):解释器的全局信息
抽象表达式(Component):定义一个接口来解释操作
终结符表达式(TerminalExpression):直接跳过步骤,不用解释语句
非终结符表达式(NonterminalExpression):根据规则实现解释操作
客户端(Client):调用解释器,对语句进行解释。
实现思路 :建立语法树,然后用语法将表达式进行解析。
类图:
应用场景 :将十六进制值解释为十进制。
分析 :如果以0X开头则将十六进制解释为十进制,否则直接输出的就是十进制不需要解释。
下面我们在控制台程序去演示一下如何使用Interpreter Pattern:
一、 场景(Context)
// 场景(Context)
class Context
{
public Context( string input)
{
this .Input = input;
}
/// <summary>
/// 输入参数
/// </summary>
public string Input { get ; set ; }
/// <summary>
/// 输出参数
/// </summary>
public int Output { get ; set ; }
/// <summary>
/// 是否是十六进制 如果是转为十进制,否则不转
/// </summary>
public bool Status { get ; set ; }
}
二、抽象表达式(Component)
// 抽象表达式类(AbstractExpression)
abstract class Expression
{
public virtual void Interpret(Context context)
{
if (context.Input.Length == 0 )
return ;
int multiresult = Multiplier(context);
if (multiresult == 0 )
return ;
if (context.Input.StartsWith( " F " ))
{
context.Output += ( 15 * multiresult);
context.Input=context.Input.Substring( 1 );
}
else if (context.Input.StartsWith( " F " ))
{
context.Output += ( 15 * multiresult);
context.Input = context.Input.Substring( 1 );
}
else if (context.Input.StartsWith( " E " ))
{
context.Output += ( 14 * multiresult);
context.Input = context.Input.Substring( 1 );
}
else if (context.Input.StartsWith( " D " ))
{
context.Output += ( 13 * multiresult);
context.Input = context.Input.Substring( 1 );
}
else if (context.Input.StartsWith( " C " ))
{
context.Output += ( 12 * multiresult);
context.Input = context.Input.Substring( 1 );
}
else if (context.Input.StartsWith( " B " ))
{
context.Output += ( 11 * multiresult);
context.Input = context.Input.Substring( 1 );
}
else if (context.Input.StartsWith( " A " ))
{
context.Output += ( 10 * multiresult);
context.Input = context.Input.Substring( 1 );
}
else
{
context.Output += ( int .Parse(context.Input.Substring( 0 , 1 )) * multiresult);
context.Input = context.Input.Substring( 1 );
}
}
// 该位置需要做的乘法值
public abstract int Multiplier(Context context);
}
三、终结符表达式(TerminalExpression)
// 终结符表达式(TerminalExpression)
class NumterminalExp : Expression
{
public override void Interpret(Context context)
{
if (context.Input.StartsWith( " 0X " ))
{
context.Input = context.Input.Substring( 2 );
context.Status = true ;
}
else
{
context.Output = int .Parse(context.Input);
context.Status = false ;
return ;
}
}
public override int Multiplier(Context context)
{
return 1 ;
}
}
四、非终结符表达式(NonterminalExpression)
// 非终结符表达式(NonterminalExpression) 千位计算
class ThousandExp : Expression
{
public override int Multiplier(Context context)
{
if (context.Input.Length == 4 &&context.Status)
return 16 * 16 * 16 ;
else
return 0 ;
}
}
// 非终结符表达式(NonterminalExpression) 百位计算
class HundredExp : Expression
{
public override int Multiplier(Context context)
{
if (context.Input.Length == 3 && context.Status)
return 16 * 16 ;
else
return 0 ;
}
}
// 非终结符表达式(NonterminalExpression) 十位计算
class TenExp : Expression
{
public override int Multiplier(Context context)
{
if (context.Input.Length == 2 && context.Status)
return 16 ;
else
return 0 ;
}
}
// 非终结符表达式(NonterminalExpression) 个位计算
class OneExp : Expression
{
public override int Multiplier(Context context)
{
if (context.Input.Length == 1 && context.Status)
return 1 ;
else
return 0 ;
}
}
五、客户端(Client)
// 客户端(Client)
class Program
{
static void Main( string [] args)
{
string input = " 0XA321 " ;
Context context = new Context(input.ToUpper());
List<Expression> expTree = new List<Expression>();
expTree.Add( new NumterminalExp());
expTree.Add( new ThousandExp());
expTree.Add( new HundredExp());
expTree.Add( new TenExp());
expTree.Add( new OneExp());
foreach (Expression exp in expTree)
{
exp.Interpret(context);
}
Console.WriteLine( " 十六进制数{0}转换为十进制数{1} " , input, context.Output);
Console.ReadLine();
}
}
如需源码请点击 InterpreterPattern.rar 下载。
分类: 极速理解设计模式系列
绿色通道: 好文要顶 关注我 收藏该文 与我联系
作者: 程兴亮
出处: http://HdhCmsTestcnblogs测试数据/chengxingliang/
本文版权归作者和博客园共有,欢迎转载,未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
当前标签: 极速理解设计模式系列
极速理解设计模式系列:24.解释器模式(Interpreter Pattern)
极速理解设计模式系列:23.装饰器模式(Decorator Pattern)
极速理解设计模式系列:22.状态模式(State Pattern)
极速理解设计模式系列:21.简单工厂模式(Simple Factory Pattern)
极速理解设计模式系列:20.模板方法模式(Template Method Pattern)
极速理解设计模式系列:19.备忘录模式(Memento Pattern)
极速理解设计模式系列【目录索引】
极速理解设计模式系列:18.访问者模式(Visitor Pattern)
极速理解设计模式系列:16.迭代器模式(Iterator Pattern)
极速理解设计模式系列:15.中介者模式(Mediator Pattern)
极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)
极速理解设计模式系列:13.外观模式(Facade Pattern)
极速理解设计模式系列:12.责任链模式(Chain of Responsibility Pattern)
极速理解设计模式系列:11.单例模式(Singleton Pattern)
极速理解设计模式系列:10.抽象工厂模式(Abstract Factory Pattern)
极速理解设计模式系列:9.工厂方法模式(Factory Method Pattern)
极速理解设计模式系列:8.策略模式(Strategy Pattern)
极速理解设计模式系列:7.桥接模式(Bridge Pattern)
极速理解设计模式系列:6.适配器模式(Adapter Pattern)
极速理解设计模式系列:5.组合模式(Composite Pattern)
极速理解设计模式系列:4.原型模式(Prototype Pattern)
极速理解设计模式系列:2.观察者模式(Observer Pattern)
极速理解设计模式系列:1.建造者模式(Builder Pattern)
作者: Leo_wl
出处: http://HdhCmsTestcnblogs测试数据/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于极速理解设计模式系列:24.解释器模式(Interpreter Pattern)的详细内容...