首页 > 代码库 > 解释器模式

解释器模式

Context.java

package edu.interpreter;

public class Context {
    private String input;
    private int output;
    public String getInput() {
        return input;
    }
    public void setInput(String input) {
        this.input = input;
    }
    public int getOutput() {
        return output;
    }
    public void setOutput(int output) {
        this.output = output;
    }
}

Expression.java

package edu.interpreter;

/*
 * 抽象解释器
 */
public abstract class Expression {
    public abstract void interpret(Context context);
}

PlusExpression.java

package edu.interpreter;

/*
 * 加法解释器
 */
public class PlusExpression extends Expression {

    @Override
    public void interpret(Context context) {
        // 提示信息
        System.out.println("自动递增");
        // 获得上下文环境
        String input = context.getInput();
        // 进行类型转换
        int intInput = Integer.parseInt(input);
        System.out.println(intInput);
        // 进行递增
        ++intInput;
        // 对上下文环境重新进行赋值
        context.setInput(intInput + "");
        context.setOutput(intInput);
    }
}

MinusExpression.java

 

package edu.interpreter;

/*
 * 减法解释器
 */
public class MinusExpression extends Expression {

    @Override
    public void interpret(Context context) {
        // 提示信息
        System.out.println("自动递减");
        // 获得上下文环境
        String input = context.getInput();
        // 进行类型转换
        int intInput = Integer.parseInt(input);
        System.out.println(intInput);
        // 进行递增
        --intInput;
        // 对上下文环境重新进行赋值
        context.setInput(intInput + "");
        context.setOutput(intInput);
    }
}

 

MainClass.java

 

package edu.interpreter;

import java.util.List;

public class MainClass {
    List<Expression> list;

    public List<Expression> getList() {
        return list;
    }

    public void setList(List<Expression> list) {
        this.list = list;
    }
    
}

 

applicationContext.java

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <bean id="plusExpression" class="edu.interpreter.PlusExpression" />
    <bean id="minusExpression" class="edu.interpreter.MinusExpression" />
    <bean id="context" class="edu.interpreter.Context">
        <property name="input" value="30" />
    </bean>
    <bean id="mainClass" class="edu.interpreter.MainClass">
        <property name="list">
            <list>
                <!-- 注入class -->
                <ref bean="plusExpression" />
                <ref bean="minusExpression" />
            </list>
        </property>
    </bean>
</beans>