设计模式_23:解释器模式

来源:互联网 发布:jsoup实例源码 编辑:程序博客网 时间:2024/06/08 11:56

书上的原句:

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

最简单的例子:

import java.util.ArrayList;import java.util.List;public class Main {    public static void main(String[] args) {        Context context = new Context();        List<AbstractExpression> list = new ArrayList<>();        list.add(new TerminalExpression());        list.add(new NonTerminalExpression());        list.add(new TerminalExpression());        list.add(new TerminalExpression());        for (AbstractExpression expression : list){            expression.interpret(context);        }    }}//Context类包含解析器之外的全局信息,通常用来作为解析器的输入和输出class Context{    private String input;    private String output;    public String getInput() {        return input;    }    public void setInput(String input) {        this.input = input;    }    public String getOutput() {        return output;    }    public void setOutput(String output) {        this.output = output;    }}//抽象解析器abstract class AbstractExpression {    abstract void interpret(Context context);}class TerminalExpression extends AbstractExpression {    @Override    void interpret(Context context) {        System.out.println("终端解释器");    }}class NonTerminalExpression extends AbstractExpression {    @Override    void interpret(Context context) {        System.out.println("非终端解释器");    }}

运行结果:

终端解释器非终端解释器终端解释器终端解释器

复杂一点的例子,做一个乐谱解析器,

public class Main {    public static void main(String[] args) {        PlayContext context = new PlayContext();        context.setText("O 3 E 0.5 G 0.5 A 3 O 3 E 0.5 G 0.5 E 3");        AbstractExpression expression = null;        while (context.getText().length() > 0){            String text = context.getText();            String c = text.substring(0, 1);            switch (c) {                case "O":                    expression = new Scale();                    break;                case "A":                case "B":                case "C":                case "D":                case "E":                case "F":                case "G":                    expression = new Note();                    break;            }            expression.interpret(context);        }    }}//乐谱class PlayContext{    private String text;    public String getText() {        return text;    }    public void setText(String text) {        this.text = text;    }}//抽象表达式abstract class AbstractExpression{    //解释playContext内容    public void interpret(PlayContext context){        if (null == context || null == context.getText() || 0 == context.getText().length())            return;        //O 3 E 0.5 G 0.5 A 3        String text = context.getText();        String key = text.substring(0, 1);                                     //key = "O"        context.setText(context.getText().substring(2));                       //text = "3 E 0.5 G 0.5 A 3"        int cutIndex = context.getText().indexOf(" ");        double playValue = Double.parseDouble(context.getText().substring(0, cutIndex == -1 ? 1 : cutIndex));  //playValue = 3        if (cutIndex != -1)            context.setText(context.getText().substring(context.getText().indexOf(" ") + 1));   //text = "E 0.5 G 0.5 A 3"        else            context.setText("");        this.excute(key, playValue);    }    abstract void excute(String key, double value);}//音阶类解释class Scale extends AbstractExpression {    @Override    void excute(String key, double value) {        String scale = "";        switch ((int)value){            case 1 :                scale = "低音";                break;            case 2 :                scale = "中音";                break;            case 3 :                scale = "高音";                break;        }        System.out.printf(scale + " ");    }}//音符类解释class Note extends AbstractExpression {    @Override    void excute(String key, double value) {        String note = "";        switch (key){            case "C" :                note = "1";                break;            case "D" :                note = "2";                break;            case "E" :                note = "3";                break;            case "F" :                note = "4";                break;            case "G" :                note = "5";                break;            case "A" :                note = "6";                break;            case "B" :                note = "7";                break;        }        System.out.printf(note + " ");    }}

运行结果:

高音 3 5 6 高音 3 5 3