[转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式

主讲:李建忠

来源:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/consyscourse/CsharpOOD.aspx

 

[转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式

[转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式

[转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式

[转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式

 

   1:   
   2:  public class MainApp
   3:  {
   4:      static void Main()
   5:      {
   6:          sting roman="五千三百零九万六千四百五十二";    
   7:   
   8:          Context context=new Context(roman);
   9:     
  10:          ArrayList tree=new ArrayList();
  11:          tree.Add(new GeExpresion());
  12:          tree.Add(new ShiExpresion());
  13:          tree.Add(new BaiExpresion());
  14:          tree.Add(new QianExpresion());
  15:          tree.Add(new WanExpresion());
  16:   
  17:          foreach(Expression exp in tree)
  18:          {
  19:             exp.Interpret(context);
  20:          }
  21:   
  22:          Console.WriteLine("{0}={1}",roman,context.Data);
  23:      }
  24:  }
  25:   
  26:  public class Context
  27:  {
  28:      private string statement;
  29:      private int data;
  30:   
  31:      public Context(statement)
  32:      {
  33:          this.statement=statement;
  34:      }
  35:   
  36:      public string Statment
  37:      {
  38:          get{return statement;}
  39:          set{statement=value;}
  40:      }
  41:   
  42:      public string Data
  43:      {
  44:          get{return data;}
  45:          set{data=value;}
  46:      }
  47:  }
  48:   
  49:  public abstract class Expression
  50:  {
  51:      protected Dictionary<string, int> table=new Dictionary<string, int>(9);
  52:      public Expression()
  53:      {
  54:          table.Add("一",1);
  55:          table.Add("二",2);
  56:          table.Add("三",3);
  57:          table.Add("四",4);
  58:          table.Add("五",5);
  59:          table.Add("六",6);
  60:          table.Add("七",7);
  61:          table.Add("八",8);
  62:          table.Add("九",9);
  63:      }
  64:   
  65:      public virtual void Interpret(Context context)
  66:      {
  67:          if(context.Statement.Length==0)
  68:          {
  69:              return;
  70:          }
  71:   
  72:          foreach(string key in table.Keys)
  73:          {
  74:              int value=table[key];
  75:   
  76:              if(context.Statement.EndsWith(key+GetPostfix()))
  77:              {
  78:                  context.Data+=value*this.Multiplier();
  79:                  context.Statement=context.Statement.Substring(0,context.Statement.Length-1);
  80:              }
  81:   
  82:              if(context.Statement.EndsWith("零"))
  83:              {
  84:                  context.Statement=context.Statement.Substring(0,context.Statement.Length-this.Getlength());
  85:              }
  86:          }
  87:      }
  88:   
  89:      public abstract string GetPostfix();
  90:      public abstract int Multiplier();
  91:   
  92:      public virtual int GetLength()
  93:      {
  94:          return this.GetPostfix().Lenght+1;
  95:      }
  96:  }
  97:   
  98:  public class WanExpression:Expression
  99:  {
 100:      public override string GetPostfix()
 101:      {
 102:          return "万";
 103:      }
 104:   
 105:      public override void Interpret(Context context)
 106:      {
 107:          if(context.Statement.Length==0)
 108:          {
 109:              return;
 110:          }
 111:   
 112:          ArrayList tree=new ArrayList();
 113:          tree.Add(new GeExpresion());
 114:          tree.Add(new ShiExpresion());
 115:          tree.Add(new BaiExpresion());
 116:          tree.Add(new QianExpresion());
 117:   
 118:          foreach(string key in table.Keys)
 119:          {
 120:              int temp=context.Data;
 121:              context.Data=0;
 122:   
 123:              context.Statement=context.Statement.Substring(0,context.Statement.Length-1);
 124:              
 125:              foreach(Expression exp in tree)
 126:              {
 127:                  exp.Interpret(context);
 128:              }
 129:   
 130:              context.Data=temp+this.Multiplier()*context.Data;
 131:          }
 132:      }
 133:   
 134:      public overrride Multiplier(){return 1000;}
 135:  }
 136:   
 137:  public class QianExpression:Expression
 138:  {
 139:      public override string GetPostfix()
 140:      {
 141:          return "千";
 142:      }
 143:   
 144:      public overrride int Multiplier(){return 1000;}
 145:  }
 146:   
 147:  public class BaiExpression:Expression
 148:  {
 149:      public override string GetPostfix()
 150:      {
 151:          return "百";
 152:      }
 153:   
 154:      public overrride int Multiplier(){return 100;}
 155:  }
 156:   
 157:  public class ShiExpression:Expression
 158:  {
 159:      public override string GetPostfix()
 160:      {
 161:          return "十";
 162:      }
 163:   
 164:      public overrride int Multiplier(){return 10;}
 165:  }
 166:   
 167:  public class GeExpression:Expression
 168:  {
 169:      public override string GetPostfix()
 170:      {
 171:          return "";
 172:      }
 173:   
 174:      public overrride int Multiplier(){return 1;}
 175:   
 176:      public override int GetLength()
 177:      {
 178:          return 1;
 179:      }
 180:  }

 

[转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式

[转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式

转载于:https://www.cnblogs.com/6DAN_HUST/archive/2012/10/17/2727085.html