• 解释器模式


    什么是解释器模式??

    举个例子

    假设现在有一个业务需求如下:

    输入一个模式公式(加减运算),然后输入模型中的参数,运算出结果。

    设计要求:

    1、公式可以运行时编辑,并且符合正常的算术书写方式。

    2、高扩展性

    抽象表达式类

     1 package sehjimoshi.interpreter;
     2 
     3 import java.util.HashMap;
     4 
     5 /**
     6  * <p>Description:</p>
     7  * 
     8  * @author Administrator
     9  * @date 2019年1月8日下午1:26:09
    10  * @version 1.0
    11  */
    12 public abstract class Expression {
    13     // 解释公式和数值,其中的var中的key值是公式中的参数,value是具体的数值
    14     public abstract int interpreter(HashMap<String,Integer> var);
    15 }

    变量解释器类

    package sehjimoshi.interpreter;
    
    import java.util.HashMap;
    
    /**
     * <p>Description:</p>
     * 
     * @author Administrator
     * @date 2019年1月8日下午1:28:13
     * @version 1.0
     */
    public class VarExpression extends Expression {
        private String key;
    
        public VarExpression(String key) {
            this.key = key;
        }
    
    
        @Override
        public int interpreter(HashMap<String, Integer> var) {
            return var.get(this.key);
        }
        
        
        
    }

    抽象运算符号解释器类

    /**
     * 
     */
    package sehjimoshi.interpreter;
    
    /**
     * <p>Description:</p>
     * 
     * @author Administrator
     * @date 2019年1月8日下午1:30:00
     * @version 1.0
     */
    public abstract class SymbolExpression extends Expression {
        protected Expression left;
        protected Expression right;
        
        public SymbolExpression(Expression left, Expression right) {
            super();
            this.left = left;
            this.right = right;
        }
        
        
    }

    加法解释器类

     1 /**
     2  * 
     3  */
     4 package sehjimoshi.interpreter;
     5 
     6 import java.util.HashMap;
     7 
     8 /**
     9  * <p>Description:</p>
    10  * 
    11  * @author Administrator
    12  * @date 2019年1月8日下午1:31:27
    13  * @version 1.0
    14  */
    15 public class AddExpression extends SymbolExpression {
    16     
    17     
    18     public AddExpression(Expression left, Expression right) {
    19         super(left, right);
    20 
    21     }
    22 
    23     @Override
    24     public int interpreter(HashMap<String, Integer> var) {
    25         return super.left.interpreter(var) + super.right.interpreter(var);
    26     }
    27 
    28 }

    减法解释器类

     1 /**
     2  * 
     3  */
     4 package sehjimoshi.interpreter;
     5 
     6 import java.util.HashMap;
     7 
     8 /**
     9  * <p>Description:</p>
    10  * 
    11  * @author Administrator
    12  * @date 2019年1月8日下午1:32:57
    13  * @version 1.0
    14  */
    15 public class SubExpression extends SymbolExpression {
    16 
    17     
    18     public SubExpression(Expression left, Expression right) {
    19         super(left, right);
    20 
    21     }
    22 
    23 
    24     @Override
    25     public int interpreter(HashMap<String, Integer> var) {
    26         return super.left.interpreter(var) - super.right.interpreter(var);
    27     }
    28 
    29 }

    解释器封装类

     1 /**
     2  * 
     3  */
     4 package sehjimoshi.interpreter;
     5 
     6 import java.util.HashMap;
     7 import java.util.Stack;
     8 
     9 /**
    10  * <p>Description:</p>
    11  * 
    12  * @author Administrator
    13  * @date 2019年1月8日下午1:34:05
    14  * @version 1.0
    15  */
    16 public class Calculator {
    17     // 定义表达式
    18     private Expression expression;
    19     
    20     public Calculator(String expStr) {
    21         // 定义一个栈
    22         Stack<Expression> stack = new Stack<Expression>();
    23         // 表达式拆分为数组
    24         char[] charArray = expStr.toCharArray();
    25         
    26         // 运算
    27         Expression left = null;
    28         Expression right = null;
    29         
    30         for (int i = 0; i < charArray.length; i++) {
    31             switch (charArray[i]) {
    32             case '+':
    33                 left = stack.pop();
    34                 right = new VarExpression(String.valueOf(charArray[++i]));
    35                 stack.push(new AddExpression(left,right));
    36                 break;
    37             case '-':
    38                 left = stack.pop();
    39                 right = new VarExpression(String.valueOf(charArray[++i]));
    40                 stack.push(new SubExpression(left, right));
    41                 break;
    42             default:
    43                 stack.push(new VarExpression(String.valueOf(charArray[i])));
    44                 
    45             }
    46         }
    47         
    48         // 把运算结果抛出来
    49         this.expression = stack.pop();
    50     }
    51     
    52     /**
    53      * 开始运算
    54      */
    55     public int run(HashMap<String,Integer> var) {
    56         return this.expression.interpreter(var);
    57     }
    58 }

    客户端模拟类

     1 /**
     2  * 
     3  */
     4 package sehjimoshi.interpreter;
     5 
     6 import java.io.BufferedReader;
     7 import java.io.IOException;
     8 import java.io.InputStreamReader;
     9 import java.util.HashMap;
    10 import java.util.Iterator;
    11 
    12 /**
    13  * <p>Description:</p>
    14  * 
    15  * @author Administrator
    16  * @date 2019年1月8日下午1:42:20
    17  * @version 1.0
    18  */
    19 public class Client {
    20 
    21     /**
    22      * @param args
    23      * @throws IOException 
    24      */
    25     public static void main(String[] args) throws IOException {
    26         // TODO Auto-generated method stub
    27         String expStr = getExpStr();
    28         
    29         HashMap<String, Integer> var = getValue(expStr);
    30         
    31         Calculator cal = new Calculator(expStr);
    32         System.out.println("运算结果:"+expStr+"="+cal.run(var));
    33     }
    34     
    35     public static String getExpStr() throws IOException {
    36         System.out.print("请输入表达式:");
    37         return (new BufferedReader(new InputStreamReader(System.in)).readLine());
    38     }
    39     
    40     public static HashMap<String,Integer> getValue(String exprStr) throws IOException {
    41         HashMap<String,Integer> map = new HashMap<String,Integer>();
    42         
    43         for(char ch:exprStr.toCharArray()) {
    44             if(ch != '+' && ch != '-') {
    45                 // 解决参数重复的问题
    46                 if(!map.containsKey(String.valueOf(ch))) {
    47                     String in = (new BufferedReader(new InputStreamReader(System.in)).readLine());
    48                     map.put(String.valueOf(ch), Integer.valueOf(in));
    49                 }
    50             }
    51         }
    52         
    53         return map;
    54     }
    55 
    56 }

    运行结果:

  • 相关阅读:
    制作自适应布局的模块及框架(转载)
    从今天起开始写博了
    工作中碰到的css问题解决方法
    标题写个什么好呢
    快速编写HTML(Zen conding)
    2013年1月21日记事
    opc 方面研究
    关于 部署方面研究 Visual Studio 2013
    intel AVX指令集
    关于 返回数据类型 后 加& 的作用
  • 原文地址:https://www.cnblogs.com/wylwyl/p/10238642.html
Copyright © 2020-2023  润新知