设计模式学习笔记(十六)Interpreter解释器(行为型模式)

动机(Motivation)

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

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


最常见的例子:正则表达式的使用。


意图(Intent)

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

示例代码

using System;
using System.Collections;
using System.Collections.Generic;

public class MainApp
{
 static void Main()
 {
  //此处为了简单没有象李建忠老师示例中写的考虑千及以上单位,例如对万的处理,解释时还要对万以上数字进行个、十、百、千的处理
  
  string roman = "四百五十二";
  
  Context context = new Context(roman);
  ArrayList tree = new ArrayList();
  tree.Add(new GeExpression());//加的时候注意顺序
  tree.Add(new ShiExpression());
  tree.Add(new BaiExpression());

  
  foreach(Expression exp in tree)
  {
   exp.Interpret(context);
  }
  Console.WriteLine("{0}={1}",roman,context.Data);
  
 }
}


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>(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.Lengh==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 GeExpression:Expression
{
 public override string GetPostfix()
 {
  return "";
 }
 public override int Multiplier(){return 1;}
 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 override string GetPostfix()
 {
  return "百";
 }
 public override int Multiplier(){return 100;}
}

 


Interpreter模式的几个要点

1)Interpreter模式的应用场合是Interpreter模式应用中的难点,只有满足“业务规则频繁变化,且类似的模式不断重复出现,并且容易抽象为语法规则的问题”才适合使用Interpreter模式。
2)使用Interpreter模式来表示文法规则,从而可以使用面向对象技巧来方便地“扩展”文法。
3)Interpreter模式比较适合简单的文法表示,对于重复的文法表示,Interpreter模式会产生比较大的类层次结构,需要求助于语法分析生成器这样的标准工具。


.NET架构中的Interpreter

正则表达式、SQL语句的解释(特定领域的语言) 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值