解釋器模式(Interpreter )
實現(xiàn)了一個表達式接口,該接口解釋一個特定的上下文
應用
編譯器,正則表達式,SQL解析
實現(xiàn)
實現(xiàn)一個一位數(shù)的加法運算文章來源:http://www.zghlxwxcb.cn/news/detail-511823.html
public class Interpreter {
public int add(String s){
if (s.charAt(1)=='+'){
return s.charAt(0)-'0' + s.charAt(2)-'0';
}
return 0;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Interpreter().add("3+4"));
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-511823.html
到了這里,關(guān)于設計模式(二十三)——解釋器模式(Interpreter )的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!