Interpreter mode-demo

achieve

We will create an interface  Expression  and  an entity class that implements the  Expression interface.

The TerminalExpression  class that defines terminal expressions  . The non-terminal expression classes  OrExpression and AndExpression are  used to create combined expressions.

InterpreterPatternDemo , our presentation class uses the  Expression  class to create rules and demonstrate expression parsing.

Interpreter pattern UML diagram

The first step is to create an expression interface 

/**
 * 表达式接口
 */
public interface Expression {
   public boolean interpret(String context);
}

The second step is to create a terminal expression

/**
 * 终端表达式
 */
public class TerminalExpression implements 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;
   }
}

The third step is to create a non-terminal expression

/**
 * 非终端表达式、或运算
 */
public class OrExpression implements Expression {
    
   private Expression expr1 = null;
   private Expression expr2 = null;
 
   public OrExpression(Expression expr1, 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 Expression {
    
   private Expression expr1 = null;
   private Expression expr2 = null;
 
   public AndExpression(Expression expr1, Expression expr2) { 
      this.expr1 = expr1;
      this.expr2 = expr2;
   }
 
   @Override
   public boolean interpret(String context) {      
      return expr1.interpret(context) && expr2.interpret(context);
   }
}

The fourth step, the test

package interpreter.demo1;

/**
 * 测试类
 */
public class Client {
 
   //规则:Robert 和 John 是男性
   public static Expression getMaleExpression(){
      Expression robert = new TerminalExpression("Robert");
      Expression john = new TerminalExpression("John");
      return new OrExpression(robert, john);    
   }
 
   //规则:Julie 是一个已婚的女性
   public static Expression getMarriedWomanExpression(){
      Expression julie = new TerminalExpression("Julie");
      Expression married = new TerminalExpression("Married");
      return new AndExpression(julie, married);    
   }
 
   public static void main(String[] args) {
      Expression isMale = getMaleExpression();
      Expression isMarriedWoman = getMarriedWomanExpression();
 
      System.out.println("John is male? " + isMale.interpret("John"));
      System.out.println("Julie is a married women? "+ isMarriedWoman.interpret("Married Julie"));
   }
}
John is male? true
Julie is a married women? true

 

Published 138 original articles · praised 34 · 150,000 views

Guess you like

Origin blog.csdn.net/bbj12345678/article/details/105362067