大话设计模式-解释器模式学习总结

一、概念

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

二、类图及基本代码

这里写图片描述

//抽象表达式
public abstract class AbstractExpression {
    public abstract void Interpret(Context context);

}
public 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;
    }



}
public class NonterminalExpression extends AbstractExpression{

    @Override
    public void Interpret(Context context) {
        System.out.println("非终端解释器");

    }

}
//终结符表达式
public class TerminalExpression extends AbstractExpression{

    @Override
    public void Interpret(Context context) {
        System.out.println("终端解释器");
    }

}
public class Client {
    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 abstractExpression : list) {
            abstractExpression.Interpret(context);
        }

    }

}

三、实例之音乐解释器

//抽象表达式类
public abstract class Expression {
    public void Interpret(PlayContext context) {
        if (context.getText().length() == 0) {
            return;
        } else {
            String playKey = context.getText().substring(0, 1);
            context.setText(context.getText().substring(2));
            double playValue = Double.valueOf(context.getText().substring(0, 1).trim());
            context.setText(context.getText().substring(context.getText().indexOf(" ") + 1));
            Excute(playKey, playValue);
        }

    }

    public abstract void Excute(String key, double value);

}
//演奏内容
public class PlayContext {
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}
//音符类
public class Note extends Expression {

    @Override
    public 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;
        default:
            break;
        }
        System.out.println(note);
    }

}
//音阶类
public class Scale extends Expression {

    @Override
    public void Excute(String key, double value) {
        String scale = "";
        switch ((int) value) {
        case 1:
            scale = "低音";
            break;
        case 2:
            scale = "中音";
            break;
        case 3:
            scale = "高音";
            break;
        default:
            break;
        }
        System.out.println(scale);
    }

}
//演奏速度
public class Speed extends Expression{

    @Override
    public void Excute(String key, double value) {
        String speed;
        if(value<500){
            speed="快速";
        }else if(value>=1000){
            speed="慢速";
        }else{
            speed="中速";
        }
        System.out.println(speed);
    }

}
/*
上海滩
中音
3
5
6
3
5
2
 */
public class Client {
    public static void main(String[] args) {
        PlayContext context = new PlayContext();

        System.out.println("上海滩");

        context.setText("O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 ");
        Expression expression = null;
        try {
            while (context.getText().length() > 0) {
                String string = context.getText().substring(0, 1);
                switch (string) {
                case "O":
                    expression = new Scale();// 为O时,实例化音阶
                    break;
                case "T":
                    expression = new Speed();
                    break;
                case "C":
                case "D":
                case "E":
                case "F":
                case "G":
                case "A":
                case "B":
                case "P":
                    expression = new Note();// 实例化音符
                    break;
                default:
                    break;
                }
                expression.Interpret(context);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

四、总结

1、使用时机。
当有一个语言需要解释执行时,并且你可以将该语言中的句子表示为一个抽象语法树时,可以使用解释器模式。
2、优点与缺点。
优点:用了解释器模式后可以容易地改变和拓展文法,如音乐播放器案例中,有演奏速度,音符,音阶三种文法,如果不需要演奏速度文法,就从客户端删除相应的case语句即可,如果要新增文法,只需新增一个类让它继承抽象表达式即可。
缺点:当文法比较复杂时就不再适合使用解释器模式,此外,解释器模式在拓展文法时,需要新增一个类并且要改动客户端。
3、优化。
对于拓展文法时需要新增一个类并改动客户端这种情况,可以通过简单工厂+反射来优化,就是将原本在客户端上的判断逻辑搬入工厂类中,并且通过反射消除判断语句。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值