解释器模式(Interpreter)(类行为型模式)

参考文档:

1.设计模式-可复用面向对象软件的基础

2.http://www.cnblogs.com/itTeacher/archive/2012/12/12/2814437.html(Java设计模式十四:解释器模式(Interpreter))


解释器模式(Interpreter)(类行为型模式)

理论方面的知识可以参考 参考文档。

看一下Interpreter的结构:

解释器模式(Interpreter)(类行为型模式)_第1张图片

代码实现:

Context.java:
package com.rick.designpattern.interpreter;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by MyPC on 2017/6/30.
 */
public class Context {
    private Map valueMap = new HashMap();

    public void addValue(String key, int value) {
        valueMap.put(key, Integer.valueOf(value));
    }

    public int getValue(String key) {
        return valueMap.get(key).intValue();
    }

}
AbstractExpression.java:
package com.rick.designpattern.interpreter;

/**
 * Created by MyPC on 2017/6/30.
 */
public abstract class AbstractExpression {
    public abstract int interpreter(Context context);
}
AddNonterminalExpression.java:
package com.rick.designpattern.interpreter;

/**
 * Created by MyPC on 2017/6/30.
 */
public class AddNonterminalExpression extends AbstractExpression {
    private AbstractExpression left, right;

    public AddNonterminalExpression(AbstractExpression left, AbstractExpression right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public int interpreter(Context context) {
        return this.left.interpreter(context) + this.right.interpreter(context);
    }
}
DivisionNonterminalExpression.java:
package com.rick.designpattern.interpreter;

/**
 * Created by MyPC on 2017/6/30.
 */
public class DivisionNonterminalExpression extends AbstractExpression {
    private AbstractExpression left, right;

    public DivisionNonterminalExpression(AbstractExpression left, AbstractExpression right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public int interpreter(Context context) {
        int value = this.right.interpreter(context);
        if (value != 0) {
            return this.left.interpreter(context) / value;
        }
        return -1111;
    }
}
MultiplyNonterminalExpression.java:
package com.rick.designpattern.interpreter;

/**
 * Created by MyPC on 2017/6/30.
 */
public class MultiplyNonterminalExpression extends AbstractExpression {
    private AbstractExpression left, right;

    public MultiplyNonterminalExpression(AbstractExpression left, AbstractExpression right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public int interpreter(Context context) {
        return this.left.interpreter(context) * this.right.interpreter(context);
    }
}
TerminalExpression.java:
package com.rick.designpattern.interpreter;

/**
 * Created by MyPC on 2017/6/30.
 */
public class TerminalExpression extends AbstractExpression {
    private int i;

    public TerminalExpression(int i) {
        this.i = i;
    }

    @Override
    public int interpreter(Context context) {
        return this.i;
    }
}
Client.java:
package com.rick.designpattern.interpreter;

/**
 * Created by MyPC on 2017/6/30.
 */
public class Client {


    public static void main(String[] args) {
        Context context = new Context();
        context.addValue("a", 5);
        context.addValue("b", 4);
        context.addValue("c", 3);
        context.addValue("d", 2);

        AbstractExpression abstractExpression = new AddNonterminalExpression(
                new TerminalExpression(context.getValue("a")), new TerminalExpression(context.getValue("b")));
        System.out.println(abstractExpression.interpreter(context));
        AbstractExpression abstractExpression2 = new DivisionNonterminalExpression(
                new TerminalExpression(context.getValue("a")), new TerminalExpression(context.getValue("b")));
        System.out.println(abstractExpression2.interpreter(context));
        AbstractExpression abstractExpression3 = new MultiplyNonterminalExpression(
                new TerminalExpression(context.getValue("a")), new TerminalExpression(context.getValue("b")));
        System.out.println(abstractExpression3.interpreter(context));
        AbstractExpression abstractExpression4 = new SubtractNonterminalExpression(
                new TerminalExpression(context.getValue("a")), new TerminalExpression(context.getValue("b")));
        System.out.println(abstractExpression4.interpreter(context));
    }

}

你可能感兴趣的:(理解设计模式)