什么是解释器模式(Interpreter)?

120 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

  1. 意图
    根据语法规则解释语言
  2. 类图
    在这里插入图片描述
  3. 实例
interface Expression {
        /**
         * 解释
         * @param context
         * @return
         */
        Boolean interpret (String context);
    }

    static class Terminal implements Expression {
        private String data;
        public Terminal (String data) {
            this.data = data;
        }

        @Override
        public Boolean interpret(String context) {
            if (null != context && context.contains(data)) {
                return true;
            }
            return false;
        }
    }

    static class And implements Expression {
        private Expression expression1;
        private Expression expression2;

        public And (Expression expression1, Expression expression2) {
            this.expression1 = expression1;
            this.expression2 = expression2;
        }

        @Override
        public Boolean interpret(String context) {
            return expression1.interpret(context) && expression2.interpret(context);
        }
    }

    static class Or implements Expression {
        private Expression expression1;
        private Expression expression2;

        private Or (Expression expression1, Expression expression2) {
            this.expression1 = expression1;
            this.expression2 = expression2;
        }

        @Override
        public Boolean interpret(String context) {
            return expression1.interpret(context) || expression2.interpret(context);
        }
    }

    interface Rule {
        /**
         * 获取男性
         * @return
         */
        Expression getMale ();

        /**
         * 获取已婚
         * @return
         */
        Expression getMarried ();
    }

    static class RuleImpl implements Rule {

        @Override
        public Expression getMale() {
            Expression expression1 = new Terminal("Robot");
            Expression expression2 = new Terminal("John");
            return new Or(expression1, expression2);
        }

        @Override
        public Expression getMarried() {
            Expression expression1 = new Terminal("Julie");
            Expression expression2 = new Terminal("Married");
            return new And(expression1, expression2);
        }
    }
  1. 测试
public static void main(String[] args) {
        Rule rule = new RuleImpl();

        Expression male = rule.getMale();
        Expression married = rule.getMarried();

        boolean b1 = male.interpret("Is John a Robot?");
        boolean b2 = married.interpret("Julie or Married?");

        System.out.println("b1 --- " + b1 + ", b2 --- " + b2);
    }

运行结果:

b1 --- true, b2 --- true

想看更多吗?请访问:设计模式