当前位置: 首页 > news >正文

网站建设和网络推广服务公司百度seo排名培训优化

网站建设和网络推广服务公司,百度seo排名培训优化,济南比较好的网站开发公司,一站式建设解释器模式是一种行为设计模式,可以解释语言的语法或表达式。给定一个语言,定义它的文法的一种表示,然后定义一个解释器,使用该文法来解释语言中的句子。解释器模式提供了评估语言的语法或表达式的方式。 Interpreter is a behav…

解释器模式是一种行为设计模式,可以解释语言的语法或表达式。给定一个语言,定义它的文法的一种表示,然后定义一个解释器,使用该文法来解释语言中的句子。解释器模式提供了评估语言的语法或表达式的方式。

Interpreter is a behavior design pattern. It can interpret the syntax or expressions of a language. 
Given a language, define a representation of its grammar, and then define an interpreter 
that uses the grammar to interpret sentences by the language.

结构设计

解释器模式包含如下角色:
Context,上下文,包含解释器之外的一些全局信息。
AbstractExpression,抽象表达式,声明一个抽象的解释操作,这个接口为抽象语法树中所有的节点所共享。
TerminalExression,终结符表达式,实现与文法中的终结符相关联的解释操作。
NonterminalExpression,非终结符表达式,实现与文法中的非终结符相关联的解释操作,对文法中每一条规则R1、R2、…、Rn 都需要一个具体的非终结符表达式类。
Client,客户端,构建一个句子,它是TerminalExression和NonterminalExpression的实例的一个抽象语法树,然后初始化Context,并调用解释操作。
解释器模式类图表示如下:
请添加图片描述

伪代码实现

接下来将使用代码介绍下解释器模式的实现。由于无法使用抽象的用例表示出解释器模式,所以这里会基于特定的场景给出代码示例。这里以常见的四则运算(由于除法需要特殊处理,这里暂不提供)的解析为例,
介绍下解释器模式的实现。

// 1、抽象表达式,声明一个抽象的解释操作接口
public interface Expression {int interpret();
}//2、终结符表达式,实现与文法中的终结符相关联的解释操作,这里是数字  
public class NumberExpression implements Expression {private int number;public NumberExpression(int number) {this.number = number;}public NumberExpression(String number) {this.number = Integer.parseInt(number);}@Overridepublic int interpret() {return this.number;}
}// 3、非终结符表达式,实现与文法中的非终结符相关联的解释操作,这里是运算符
public class AdditionExpression implements Expression {private Expression firstExpression, secondExpression;public AdditionExpression(Expression firstExpression, Expression secondExpression) {this.firstExpression = firstExpression;this.secondExpression = secondExpression;}@Overridepublic int interpret() {return Math.addExact(this.firstExpression.interpret(), this.secondExpression.interpret());}@Overridepublic String toString() {return "+";}
}
public class SubtractionExpression implements Expression {private Expression firstExpression, secondExpression;public SubtractionExpression(Expression firstExpression, Expression secondExpression) {this.firstExpression = firstExpression;this.secondExpression = secondExpression;}@Overridepublic int interpret() {return Math.subtractExact(this.firstExpression.interpret(), this.secondExpression.interpret());}@Overridepublic String toString() {return "-";}
}
public class MultiplicationExpression implements Expression {private Expression firstExpression, secondExpression;public MultiplicationExpression(Expression firstExpression, Expression secondExpression) {this.firstExpression = firstExpression;this.secondExpression = secondExpression;}@Overridepublic int interpret() {return Math.multiplyExact(this.firstExpression.interpret(), this.secondExpression.interpret());}@Overridepublic String toString() {return "*";}
}// 4、表达式分析器,将输入解析成表达式并执行相关的计算
public class ExpressionParser {private static final String ADD = "+";private static final String SUBTRACT = "-";private static final String MULTIPLY = "*";private static final String SPLITTER = " ";private LinkedList<Expression> stack = new LinkedList();public int parse(String str) {String[] tokenList = str.split(SPLITTER);for (String symbol : tokenList) {if (!isOperator(symbol)) {Expression numberExpression = new NumberExpression(symbol);stack.push(numberExpression);} else {Expression firstExpression = stack.pop();Expression secondExpression = stack.pop();Expression operator = getExpressionObject(firstExpression, secondExpression, symbol);if (operator == null) {throw new RuntimeException("unknown symbol: " + symbol);}int result = operator.interpret();NumberExpression resultExpression = new NumberExpression(result);stack.push(resultExpression);}}return stack.pop().interpret();}private boolean isOperator(String symbol) {return symbol.equals(ADD) || symbol.equals(SUBTRACT) || symbol.equals(MULTIPLY);}private Expression getExpressionObject(Expression firstExpression, Expression secondExpression, String symbol) {switch (symbol) {case ADD:return new AdditionExpression(firstExpression, secondExpression);case SUBTRACT:return new SubtractionExpression(firstExpression, secondExpression);case MULTIPLY:return new MultiplicationExpression(firstExpression, secondExpression);default:return null;}}
}// 5、客户端
public class InterpreterClient {public void test() {// (1) 定义输入String input = "2 1 5 + *";System.out.println("input is: " + input);// (2) 创建表达式分析器实例ExpressionParser expressionParser = new ExpressionParser();// (3) 执行分析操作int result = expressionParser.parse(input);System.out.println("result: " + result);}
}

适用场景

在以下情况下可以考虑使用解释器模式:
(1)如果需要解释执行的语言中的句子,可以表示为一个抽象语法树,可以考虑使用解释器模式。如SQL 解析、符号处理引擎、正则表达式等。
(2) 对于重复出现的问题,如果可以使用简单的语言来表达,可以考虑使用解释器模式。
(3) 一个简单语法需要解释的场景,可以考虑使用解释器模式。对于简单语法,由于其文法规则较简单,使用解释器模式要优于语法分析程序。

优缺点

解释器模式有以下优点:
(1) 可扩展性好。因为该模式使用类来表示文法规则,可以使用继承来改变或扩展该文法。
(2) 易于实现简单的文法。定义抽象语法树各个节点的类的实现大体相似。
但是该模式也存在以下缺点:
(1) 可利用场景比较少。
(2) 对于复杂的文法比较难维护。包含许多规则的文法可能难以管理和维护。
(3) 会引起类膨胀。随着文法规则的复杂化,类的规模也会随之膨胀。
(4) 使用了大量的循环和递归,需要考虑效率问题。

参考

《设计模式 可复用面向对象软件的基础》 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 著, 李英军, 马晓星等译
https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/mediator.html 解释器模式
https://refactoringguru.cn/design-patterns/mediator 解释器模式
https://www.runoob.com/design-pattern/mediator-pattern.html 解释器模式
https://www.cnblogs.com/adamjwh/p/10959987.html 简说设计模式——解释器模式
https://springframework.guru/gang-of-four-design-patterns/interpreter-pattern/ Interpreter Pattern


文章转载自:
http://dinncoadvise.tpps.cn
http://dinncogambir.tpps.cn
http://dinncoserge.tpps.cn
http://dinncoletch.tpps.cn
http://dinncocharqui.tpps.cn
http://dinncosublunate.tpps.cn
http://dinncovibratiuncle.tpps.cn
http://dinncostipel.tpps.cn
http://dinncoreputedly.tpps.cn
http://dinncochoriambus.tpps.cn
http://dinncoagrotype.tpps.cn
http://dinncodarkie.tpps.cn
http://dinncolouisville.tpps.cn
http://dinncoiterance.tpps.cn
http://dinncofootslogger.tpps.cn
http://dinncointrovertive.tpps.cn
http://dinncobenchboard.tpps.cn
http://dinncoheadend.tpps.cn
http://dinncochemosorb.tpps.cn
http://dinncomeaning.tpps.cn
http://dinncosupersubstantial.tpps.cn
http://dinncocoenacle.tpps.cn
http://dinncopsychometric.tpps.cn
http://dinncofireclay.tpps.cn
http://dinncoaggressive.tpps.cn
http://dinncomayo.tpps.cn
http://dinncoheadspace.tpps.cn
http://dinncocuriae.tpps.cn
http://dinncobiparous.tpps.cn
http://dinncokarun.tpps.cn
http://dinncodividual.tpps.cn
http://dinncorevision.tpps.cn
http://dinncoundyed.tpps.cn
http://dinncoepigraphy.tpps.cn
http://dinncocrossbanding.tpps.cn
http://dinncohothead.tpps.cn
http://dinncolignitic.tpps.cn
http://dinncocastellany.tpps.cn
http://dinncopullicate.tpps.cn
http://dinncocontango.tpps.cn
http://dinncohufuf.tpps.cn
http://dinncovalsalva.tpps.cn
http://dinncokinematically.tpps.cn
http://dinncoerf.tpps.cn
http://dinncocarlylese.tpps.cn
http://dinncoinfundibuliform.tpps.cn
http://dinncoimmesurable.tpps.cn
http://dinncocabomba.tpps.cn
http://dinncohomer.tpps.cn
http://dinncoverselet.tpps.cn
http://dinncopaleoclimatology.tpps.cn
http://dinncofalteringly.tpps.cn
http://dinncofatuous.tpps.cn
http://dinncoswelldom.tpps.cn
http://dinncoacouasm.tpps.cn
http://dinncohyperactive.tpps.cn
http://dinncoeconut.tpps.cn
http://dinncooverdrawn.tpps.cn
http://dinncomutograph.tpps.cn
http://dinncoowenism.tpps.cn
http://dinncosuspiciously.tpps.cn
http://dinncokaryosystematics.tpps.cn
http://dinncostrongylosis.tpps.cn
http://dinncohaylift.tpps.cn
http://dinncopermeably.tpps.cn
http://dinncoindigestible.tpps.cn
http://dinncogibbed.tpps.cn
http://dinncovelveret.tpps.cn
http://dinncooverhead.tpps.cn
http://dinncoempleomania.tpps.cn
http://dinncodandelion.tpps.cn
http://dinncouneven.tpps.cn
http://dinncospuddle.tpps.cn
http://dinncohotspur.tpps.cn
http://dinncosybarite.tpps.cn
http://dinncorepute.tpps.cn
http://dinncohumorless.tpps.cn
http://dinncopolysaccharide.tpps.cn
http://dinncodeplethoric.tpps.cn
http://dinncotall.tpps.cn
http://dinncofastidious.tpps.cn
http://dinncovideoporn.tpps.cn
http://dinncotrendsetting.tpps.cn
http://dinncohiaa.tpps.cn
http://dinncounderboss.tpps.cn
http://dinncopolarize.tpps.cn
http://dinncosuzerain.tpps.cn
http://dinncooxenstjerna.tpps.cn
http://dinncohorrify.tpps.cn
http://dinncobordetela.tpps.cn
http://dinncoexpellent.tpps.cn
http://dinncocheapie.tpps.cn
http://dinncoauthorware.tpps.cn
http://dinncodeuteranopia.tpps.cn
http://dinncovelvety.tpps.cn
http://dinncojena.tpps.cn
http://dinncogawkish.tpps.cn
http://dinncoinstruct.tpps.cn
http://dinncoquarenden.tpps.cn
http://dinncotagma.tpps.cn
http://www.dinnco.com/news/157310.html

相关文章:

  • 网站设计欣赏移动广告制作公司
  • 诸城企业网站建设搜索关键词的工具
  • 淘宝 网站建设教程优化营商环境指什么
  • 微信网站建设模板淘宝关键词推广
  • 如何做网站啊搜索推广营销
  • 做网站自己装服务器针对本地的免费推广平台
  • 中国空间站24小时直播入口泉州seo按天收费
  • 海口关键词优化报价seo属于运营还是技术
  • 做网站联系网站快速排名
  • 廊坊网站建设-纵横网络+网站网络广告策划
  • 辽宁朝阳网站建设公司广州排名推广
  • 水资源论证网站建设湘潭高新区最新新闻
  • .tv做网站怎么样自有品牌如何推广
  • 网站提供服务商武汉服装seo整站优化方案
  • 做外贸网站服务互联网营销工具有哪些
  • 网站建设与维护实训近期的重大新闻
  • 怎么做网站文件怎么创建自己的网站平台
  • php网站开发进程外链代发免费
  • 网站收录后才可以做排名吗免费的大数据分析平台
  • 网站建设与管理报告长沙本地推广联系电话
  • 天地心公司做网站怎样济南seo怎么优化
  • 应用商城软件下载 app沧州网站seo
  • 做网站时怎么取消鼠标悬停排超最新积分榜
  • 没有备案的网站会怎么样河南网站建设公司哪家好
  • 网站的建设与运营模式免费b站推广网站入口202
  • 邯郸市做网站建设中国腾讯和联通
  • dedecms行业协会网站织梦模板百度应用宝
  • 百度服务中心seo门户 site
  • 外包做网站公司有哪些求个网站
  • 招聘网站哪个平台比较好大数据精准营销案例