解释器模式____学习笔记

解释器模式

    /**
     * 解释器模式
     *      给定一个语言,定义它的文法的一种表示,并定义一个解释器,
     *      这个解释器使用该表示来解释语言中的句子.
     *
     *  应用场景:
     *      如果一个特定类型的问题发生的频率发生地足够高,那么就值得将该问题的各个实例表示为一个简单语言的句子.
     *      在定义一个解释器,通过该解释器来解释这个句子从而实现该问题.
     *
     *      就如同你开发了一个编程语言或脚本给自己或别人用,难度颇高
     *      通常当有一个语言需要解释执行,并且你可以将语言中的句子表示为一个抽象的语法时,可以使用解释器模式
     *
     *  作用:
     *      可以很容易地扩展和改变语法,因为该模式使用类来表示语法规则,你可以使用继承来改变或扩展该语法,
     *      也可以比较容易实现语法,因为定义抽象语法树中的各个节点的类的实现大体类似,这些都易于直接编写
     *
     *  缺点:
     *      解释器模式为每一个语法都定义了一个类,因此包含多个规则的语法可能难以维护.
     *      建议单反语法非常复杂时,其实其他的技术如语法分析程序或编译器来处理
     */

结构图

这里写图片描述

范例

public interface AbstractExpression {
    void interpret(Context context);
}
public class Context {
    private String inputString;
    private String outputString;

    public Context(String inputString, String outputString) {
        this.inputString = inputString;
        this.outputString = outputString;
    }

    public Context() {
    }

    public String getInputString() {
        return inputString;
    }

    public void setInputString(String inputString) {
        this.inputString = inputString;
    }

    public String getOutputString() {
        return outputString;
    }

    public void setOutputString(String outputString) {
        this.outputString = outputString;
    }
}
public class NonterminalExpression implements AbstractExpression {
    @Override
    public void interpret(Context context) {
        Log.d("meee","--非终端解释器");
    }
}
public class TerminalExpression implements AbstractExpression {
    @Override
    public void interpret(Context context) {
        Log.d("meee","终端解释器");
    }
}
//客户端调用
        Context context = new Context();
        List<AbstractExpression> list=new ArrayList<>();
        list.add(new NonterminalExpression());
        list.add(new NonterminalExpression());
        list.add(new TerminalExpression());
        list.add(new NonterminalExpression());

        for (AbstractExpression expression:list){
            expression.interpret(context);
        }

音乐解释器

public class PlayContext {
    private String playText;

    public PlayContext() {
    }

    public PlayContext(String playText) {
        this.playText = playText;
    }

    public String getPlayText() {
        return playText;
    }

    public void setPlayText(String playText) {
        this.playText = playText;
    }
}
public abstract class PlayInterpreter {
    public void interpret(PlayContext context,int key){
        char c = context.getPlayText().charAt(key);
        voice(c);
    }
    protected abstract void voice(char voiceChar);
}
public class MusicInterpreter extends PlayInterpreter {
    @Override
    protected void voice(char voiceChar) {
        String str=null;
        switch (voiceChar){
            case '1':
                str="多";
                break;
            case '2':
                str="来";
                break;
            case '3':
                str="米";
                break;
            case '4':
                str="发";
                break;
            case '5':
                str="嗦";
                break;
            case '6':
                str="啦";
                break;
            case '7':
                str="希";
                break;
        }
        Log.d("meee",str);
    }
}
//客户端调用
        PlayContext playContext = new PlayContext("1234567876543215413187319287104980123");
        for (int i = 0; i < playContext.getPlayText().length(); i++) {
            char c = playContext.getPlayText().charAt(i);
            switch (c){
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                    new MusicInterpreter().interpret(playContext,i);
                    break;
                default:
                    Log.d("meee","未能解析 等待更新解释器");
                    break;
            }
        }
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 多
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 来
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 米
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 发
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 嗦
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 啦
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 希
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 未能解析 等待更新解释器
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 希
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 啦
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 嗦
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 发
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 米
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 来
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 多
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 嗦
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 发
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 多
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 米
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 多
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 未能解析 等待更新解释器
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 希
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 米
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 多
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 未能解析 等待更新解释器
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 来
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 未能解析 等待更新解释器
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 希
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 多
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 未能解析 等待更新解释器
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 发
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 未能解析 等待更新解释器
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 未能解析 等待更新解释器
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 未能解析 等待更新解释器
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 多
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 来
04-13 11:07:59.775 14056-14056/com.junx.designpattem D/meee: 米
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值