怎么做专题网站,高端建站和普通建站有哪些不同,网络服务器系统是什么,网站开发相关书籍资料解释器模式
说明
解释器模式#xff08;Interpreter Pattern#xff09;属于行为型模式#xff0c;是指给定一门语言#xff0c;定义它的语法#xff08;文法#xff09;的一种表示#xff0c;并定义一个解释器#xff0c;该解释器使用该表示来解释语言中的句子。是一…解释器模式
说明
解释器模式Interpreter Pattern属于行为型模式是指给定一门语言定义它的语法文法的一种表示并定义一个解释器该解释器使用该表示来解释语言中的句子。是一种按照规定的语法文法对语言进行解析的模式。 我们平时开发用的编译器就是一种解释器的实现通过规定好的编程语法通过解析后转换成机器码才能让计算机执行。某些领域也有其规范的语法比如乐谱、莫斯码等等。解释器模式在开发中几乎不会用到除非需要创建一门新的语言。
结构
解释器模式主要角色如下 抽象表达式Expression定义解释语法的接口 终结符表达式TerminalExpression实现Expression定义的解释方法。终结符表达式可以理解为语法中需要被操作的对象比如1 21跟2都是终结符表达式 非终结符表达式NonterminalExpression实现Expression定义的解释方法。非终结符表达式可以理解为语法中的运算符或者关键字根据需求有不同的实现 上下文环境类Context包含解释器之外的信息与逻辑负责根据语法调用不同的解释器等功能。
代码案例
抽象表达式Expression
/*** program: interpreter* description: 抽象表达式接口* author: wxw* create: 2024-03-13 18:33**/
public interface IExpression {String interpret();
}非终结符表达式NonterminalExpression
/*** program: interpreter* description: 处理字符串表达式* 非终结符表达式NonterminalExpression* author: wxw* create: 2024-03-13 18:36**/
public abstract class ProcessingExpression implements IExpression {protected IExpression str;public ProcessingExpression(IExpression str) {this.str str;}
}/*** program: interpreter* description: 转换大写-处理字符串表达式* 非终结符表达式NonterminalExpression* author: wxw* create: 2024-03-13 18:44**/
public class UppercaseExpression extends ProcessingExpression {public UppercaseExpression(IExpression str) {super(str);}Overridepublic String interpret() {return str.interpret().toUpperCase();}
}/*** program: interpreter* description: 转换小写-处理字符串表达式* 非终结符表达式NonterminalExpression* author: wxw* create: 2024-03-13 18:44**/
public class LowercaseExpression extends ProcessingExpression {public LowercaseExpression(IExpression str) {super(str);}Overridepublic String interpret() {return str.interpret().toLowerCase();}
}/*** program: interpreter* description: 转换成char数组-处理字符串表达式* 非终结符表达式NonterminalExpression* author: wxw* create: 2024-03-13 18:44**/
public class CharsExpression extends ProcessingExpression {public CharsExpression(IExpression str) {super(str);}Overridepublic String interpret() {return Arrays.toString(str.interpret().toCharArray());}
}终结符表达式TerminalExpression
/*** program: interpreter* description: 字符串终结表达式* 终结符表达式TerminalExpression* author: wxw* create: 2024-03-13 18:35**/
public class StringExpression implements IExpression {private String str;public StringExpression(String str) {this.str str;}Overridepublic String interpret() {return this.str;}
}上下文环境类
/*** program: interpreter* description: 上下文环境类* author: wxw* create: 2024-03-13 18:38**/
public class StringContext {private String str;public StringContext(String str) {this.str str;}public String handleStr(){String[] strs str.split( );String key strs[0];IExpression str new StringExpression(strs[1]);switch (key){case uppercase:return new UppercaseExpression(str).interpret();case lowercase:return new LowercaseExpression(str).interpret();case chars:return new CharsExpression(str).interpret();default:return 输入错误;}}
}客户端
public class Test {public static void main(String[] args) {String str1 uppercase hello!!!;String str2 lowercase Hello!!!;String str3 chars hello!!!;StringContext stringContext1 new StringContext(str1);System.out.println(stringContext1.handleStr());StringContext stringContext2 new StringContext(str2);System.out.println(stringContext2.handleStr());StringContext stringContext3 new StringContext(str3);System.out.println(stringContext3.handleStr());}
}输出结果
HELLO!!!
hello!!!
[h, e, l, l, o, !, !, !]