设计模式(十六)解释器模式

一.定义与类型

1.定义:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子

为了解释一种语言,而为语言创建的解释器

2.类型:行为型

二.适应类型

1.在某个特定类型问题发生频率足够高

三.优点

语法由很多类表示,容易改变及扩展此“语言”

四.缺点

当语法规则数目太多时,增加了系统复杂度

五.UML类图

六.代码

Interpreter.java
package com.caomingyu.autotest.interpreter;

public interface Interpreter {
    int interpret();
}
AddInterpreter.java
package com.caomingyu.autotest.interpreter;

public class AddInterpreter implements Interpreter {
    private Interpreter firstExpression,secondExpression;

    public AddInterpreter(Interpreter firstExpression, Interpreter secondExpression) {
        this.firstExpression = firstExpression;
        this.secondExpression = secondExpression;
    }

    public int interpret() {
        return firstExpression.interpret() + secondExpression.interpret();
    }

    @Override
    public String toString() {
        return "+";
    }
}
MultiInterpreter.java
package com.caomingyu.autotest.interpreter;

public class MultiInterpreter implements Interpreter {

    private Interpreter firstExpression,secondExpression;

    public MultiInterpreter(Interpreter firstExpression, Interpreter secondExpression) {
        this.firstExpression = firstExpression;
        this.secondExpression = secondExpression;
    }

    public int interpret() {
        return firstExpression.interpret() * secondExpression.interpret();
    }

    @Override
    public String toString() {
        return "*";
    }
}
NumberInterpreter.java
package com.caomingyu.autotest.interpreter;

public class NumberInterpreter implements Interpreter {
    private int number;

    public NumberInterpreter(int number) {
        this.number = number;
    }

    public NumberInterpreter(String number) {
        this.number = Integer.parseInt(number);
    }

    public int interpret() {
        return this.number;
    }
}
CExpressionParser.java
package com.caomingyu.autotest.interpreter;

import java.util.Stack;

public class CExpressionParser {
    private Stack<Interpreter> stack = new Stack<Interpreter>();

    public int parse(String str){
        String[] strItemArray = str.split(" ");
        for (String symbol: strItemArray){
            if (!OperatorUtil.isOperator(symbol)){
                Interpreter numberExpression = new NumberInterpreter(symbol);
                stack.push(numberExpression);
                System.out.println(String.format("入栈:%d",numberExpression.interpret()));
            } else {
                //是可运算符号可以计算
                Interpreter firstExpression = stack.pop();
                Interpreter secondExpression = stack.pop();
                System.out.println(String.format("出栈:%d 和 %d",firstExpression.interpret(),secondExpression.interpret()));
                Interpreter operator = OperatorUtil.getExpressionObject(firstExpression, secondExpression, symbol);
                System.out.println(String.format("应用运算符:%s",operator));
                int result = operator.interpret();
                NumberInterpreter resultExpression = new NumberInterpreter(result);
                stack.push(resultExpression);
                System.out.println(String.format("阶段结果入栈:%d",resultExpression.interpret()));
            }
        }
        int result = stack.pop().interpret();
        return result;
    }
}
OperatorUtil.java
package com.caomingyu.autotest.interpreter;

public class OperatorUtil {
    public static boolean isOperator(String symbol) {
        return symbol.equals("+") || symbol.equals("*");
    }


    public static Interpreter getExpressionObject(Interpreter firstExpression, Interpreter secondExpression, String symbol) {
        if ("+".equals(symbol)){
            return new AddInterpreter(firstExpression, secondExpression);
        } else if ("*".equals(symbol)){
            return new MultiInterpreter(firstExpression, secondExpression);
        }
        return null;
    }
}
Test.java
package com.caomingyu.autotest.interpreter;

public class Test {
    public static void main(String[] args) {
        String cInputStr = "6 100 11 + *";
        CExpressionParser expressionParser = new CExpressionParser();
        int result = expressionParser.parse(cInputStr);
        System.out.println("解释器计算结果:"+result);
    }
}

该设计模式用的比较少,我们平时很少写解释器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值