4.2Java设计模式快速入门之解释器模式

4.2.1概念

解释器模式(Interpreter Pattern)提供了评估语言的语法或表达式的方式,它属于行为型模式。这种模式实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等。

4.2.2意图

给定一个语言,定义它的文法表示,并定义一个解释器,这个解释器使用该标识来解释语言中的句子。

4.2.3主要解决

对于一些固定文法构建一个解释句子的解释器。

4.2.4何时使用

如果一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表述为一个简单语言中的句子。这样就可以构建一个解释器,该解释器通过解释这些句子来解决该问题。

4.2.5如何解决

构建语法树,定义终结符与非终结符。

4.2.6关键代码

构建环境类,包含解释器之外的一些全局信息,一般是 HashMap。

4.2.7应用实例

编译器、运算表达式计算。

4.2.8优点

  • 可扩展性比较好,灵活。
  • 增加了新的解释表达式的方式。
  • 易于实现简单文法。

4.2.9缺点

  • 可利用场景比较少。
  • 对于复杂的文法比较难维护。
  • 解释器模式会引起类膨胀。
  • 解释器模式采用递归调用方法。

4.2.9使用场景

  • 可以将一个需要解释执行的语言中的句子表示为一个抽象语法树。
  • 一些重复出现的问题可以用一种简单的语言来进行表达。
  • 一个简单语法需要解释的场景。

4.2.10注意事项

  • 可利用场景比较少,JAVA 中如果碰到可以用 expression4J 代替。

4.2.11实现

我们将创建一个接口 com.xzy.DesignMode.Code.InterpreterPattern.Expression 和实现了 com.xzy.DesignMode.Code.InterpreterPattern.Expression 接口的实体类。定义作为上下文中主要解释器的 TerminalExpression 类。其他的类 OrExpression、AndExpression 用于创建组合式表达式。
InterpreterPatternDemo,我们的演示类使用 com.xzy.DesignMode.Code.InterpreterPattern.Expression 类创建规则和演示表达式的解析。

步骤1创建一个表达式接口。
public interface com.xzy.DesignMode.Code.InterpreterPattern.Expression {
   public boolean interpret(String context);
}
步骤2创建实现了上述接口的实体类。
public class TerminalExpression implements com.xzy.DesignMode.Code.InterpreterPattern.Expression {
   
   private String data;
 
   public TerminalExpression(String data){
      this.data = data; 
   }
 
   @Override
   public boolean interpret(String context) {
      if(context.contains(data)){
         return true;
      }
      return false;
   }
}


public class OrExpression implements com.xzy.DesignMode.Code.InterpreterPattern.Expression {
    
   private com.xzy.DesignMode.Code.InterpreterPattern.Expression expr1 = null;
   private com.xzy.DesignMode.Code.InterpreterPattern.Expression expr2 = null;
 
   public OrExpression(com.xzy.DesignMode.Code.InterpreterPattern.Expression expr1, com.xzy.DesignMode.Code.InterpreterPattern.Expression expr2) { 
      this.expr1 = expr1;
      this.expr2 = expr2;
   }
 
   @Override
   public boolean interpret(String context) {      
      return expr1.interpret(context) || expr2.interpret(context);
   }
}


public class AndExpression implements com.xzy.DesignMode.Code.InterpreterPattern.Expression {
    
   private com.xzy.DesignMode.Code.InterpreterPattern.Expression expr1 = null;
   private com.xzy.DesignMode.Code.InterpreterPattern.Expression expr2 = null;
 
   public AndExpression(com.xzy.DesignMode.Code.InterpreterPattern.Expression expr1, com.xzy.DesignMode.Code.InterpreterPattern.Expression expr2) { 
      this.expr1 = expr1;
      this.expr2 = expr2;
   }
 
   @Override
   public boolean interpret(String context) {      
      return expr1.interpret(context) && expr2.interpret(context);
   }
}
步骤3InterpreterPatternDemo 使用 com.xzy.DesignMode.Code.InterpreterPattern.Expression 类来创建规则,并解析它们。
public class InterpreterPatternDemo {
 
   //规则:Robert 和 John 是男性
   public static com.xzy.DesignMode.Code.InterpreterPattern.Expression getMaleExpression(){
      com.xzy.DesignMode.Code.InterpreterPattern.Expression robert = new TerminalExpression("Robert");
      com.xzy.DesignMode.Code.InterpreterPattern.Expression john = new TerminalExpression("John");
      return new OrExpression(robert, john);    
   }
 
   //规则:Julie 是一个已婚的女性
   public static com.xzy.DesignMode.Code.InterpreterPattern.Expression getMarriedWomanExpression(){
      com.xzy.DesignMode.Code.InterpreterPattern.Expression julie = new TerminalExpression("Julie");
      com.xzy.DesignMode.Code.InterpreterPattern.Expression married = new TerminalExpression("Married");
      return new AndExpression(julie, married);    
   }
 
   public static void main(String[] args) {
      com.xzy.DesignMode.Code.InterpreterPattern.Expression isMale = getMaleExpression();
      com.xzy.DesignMode.Code.InterpreterPattern.Expression isMarriedWoman = getMarriedWomanExpression();
 
      System.out.println("John is male? " + isMale.interpret("John"));
      System.out.println("Julie is a married women? " 
      + isMarriedWoman.interpret("Married Julie"));
   }
}
步骤4执行程序,输出结果:
John is male? true
Julie is a married women? true
步骤5查看类关系图。

Java写一个Java解释器 java实现解释器_解释器模式