码迷,mamicode.com
首页 > 其他好文 > 详细

解释器模式

时间:2014-05-19 14:56:22      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

Context.java

bubuko.com,布布扣
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;
    }
}
bubuko.com,布布扣

Expression.java

bubuko.com,布布扣
package edu.interpreter;

/*
 * 抽象解释器
 */
public abstract class Expression {
    public abstract void interpret(Context context);
}
bubuko.com,布布扣

PlusExpression.java

bubuko.com,布布扣
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);
    }
}
bubuko.com,布布扣

MinusExpression.java

 

bubuko.com,布布扣
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);
    }
}
bubuko.com,布布扣

 

MainClass.java

 

bubuko.com,布布扣
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;
    }
    
}
bubuko.com,布布扣

 

applicationContext.java

bubuko.com,布布扣
<?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>
bubuko.com,布布扣

bubuko.com,布布扣

 

解释器模式,布布扣,bubuko.com

解释器模式

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/jianfengyun/p/3735269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!