设计模式——解释器模式(Interpreter Pattern)+ Spring相关源码

文章目录

  • 一、解释器模式定义
  • 二、例子
    • 2.1 菜鸟教程例子
      • 2.1.1 定义一个表达式接口
      • 2.1.2 实现Expression接口
      • 2.1.3 定义解析规则
      • 2.1.4 使用
    • 2.2 JDK源码——Pattern
    • 2.3 Spring源码——ExpressionParser
  • 三、其他设计模式

一、解释器模式定义

类型: 行为型模式
目的: 实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等

二、例子

2.1 菜鸟教程例子

2.1.1 定义一个表达式接口

public interface Expression {public boolean interpret(String context);
}

2.1.2 实现Expression接口

public class TerminalExpression implements Expression {private String data;public TerminalExpression(String data){this.data = data; }@Overridepublic boolean interpret(String context) {if(context.contains(data)){return true;}return false;}
}
public class OrExpression implements Expression {private Expression expr1 = null;private Expression expr2 = null;public OrExpression(Expression expr1, Expression expr2) { this.expr1 = expr1;this.expr2 = expr2;}@Overridepublic boolean interpret(String context) {      return expr1.interpret(context) || expr2.interpret(context);}
}
public class AndExpression implements Expression {private Expression expr1 = null;private Expression expr2 = null;public AndExpression(Expression expr1, Expression expr2) { this.expr1 = expr1;this.expr2 = expr2;}@Overridepublic boolean interpret(String context) {      return expr1.interpret(context) && expr2.interpret(context);}
}

2.1.3 定义解析规则

public class Expressions {//规则:Robert 和 John 是男性public static Expression getMaleExpression(){Expression robert = new TerminalExpression("Robert");Expression john = new TerminalExpression("John");return new OrExpression(robert, john);    }//规则:Julie 是一个已婚的女性public static Expression getMarriedWomanExpression(){Expression julie = new TerminalExpression("Julie");Expression married = new TerminalExpression("Married");return new AndExpression(julie, married);    }
}

2.1.4 使用

public class InterpreterPatternDemo {public static void main(String[] args) {Expression isMale = Expressions.getMaleExpression();System.out.println("John is male? " + isMale.interpret("John"));Expression isMarriedWoman = Expressions.getMarriedWomanExpression();System.out.println("Julie is a married women? " + isMarriedWoman.interpret("Married Julie"));}
}

2.2 JDK源码——Pattern

public final class Pattern implements java.io.Serializable {public static Pattern compile(String regex) {return new Pattern(regex, 0);}public static Pattern compile(String regex, int flags) {return new Pattern(regex, flags);}private String pattern;public String pattern() {return pattern;}private Pattern(String p, int f) {if ((f & ~ALL_FLAGS) != 0) {throw new IllegalArgumentException("Unknown flag 0x" + Integer.toHexString(f));}pattern = p;flags = f;// to use UNICODE_CASE if UNICODE_CHARACTER_CLASS presentif ((flags & UNICODE_CHARACTER_CLASS) != 0)flags |= UNICODE_CASE;// 'flags' for compilingflags0 = flags;// Reset group index countcapturingGroupCount = 1;localCount = 0;localTCNCount = 0;if (!pattern.isEmpty()) {try {compile();} catch (StackOverflowError soe) {throw error("Stack overflow during pattern compilation");}} else {root = new Start(lastAccept);matchRoot = lastAccept;}}  private void compile() {.....}
}
static class Start extends Node {int minLength;Start(Node node) {this.next = node;TreeInfo info = new TreeInfo();next.study(info);minLength = info.minLength;}......
}
static final class StartS extends Start {StartS(Node node) {super(node);}
}
static final class Begin extends Node {boolean match(Matcher matcher, int i, CharSequence seq) {.......}
}
static final class End extends Node {boolean match(Matcher matcher, int i, CharSequence seq) {...}
}

2.3 Spring源码——ExpressionParser

public interface ExpressionParser {Expression parseExpression(String expressionString) throws ParseException;Expression parseExpression(String expressionString, ParserContext context) throws ParseException;
}
public class SpelExpressionParser extends TemplateAwareExpressionParser {private final SpelParserConfiguration configuration;public SpelExpressionParser() {this.configuration = new SpelParserConfiguration();}public SpelExpressionParser(SpelParserConfiguration configuration) {Assert.notNull(configuration, "SpelParserConfiguration must not be null");this.configuration = configuration;}public SpelExpression parseRaw(String expressionString) throws ParseException {Assert.hasText(expressionString, "'expressionString' must not be null or blank");return this.doParseExpression(expressionString, (ParserContext)null);}protected SpelExpression doParseExpression(String expressionString, @Nullable ParserContext context) throws ParseException {return (new InternalSpelExpressionParser(this.configuration)).doParseExpression(expressionString, context);}
}
class InternalSpelExpressionParser extends TemplateAwareExpressionParser {private final Deque<SpelNodeImpl> constructedNodes = new ArrayDeque();protected SpelExpression doParseExpression(String expressionString, @Nullable ParserContext context) throws ParseException {this.checkExpressionLength(expressionString);try {this.expressionString = expressionString;Tokenizer tokenizer = new Tokenizer(expressionString);this.tokenStream = tokenizer.process();this.tokenStreamLength = this.tokenStream.size();this.tokenStreamPointer = 0;this.constructedNodes.clear();SpelNodeImpl ast = this.eatExpression();if (ast == null) {throw new SpelParseException(this.expressionString, 0, SpelMessage.OOD, new Object[0]);} else {Token t = this.peekToken();if (t != null) {throw new SpelParseException(this.expressionString, t.startPos, SpelMessage.MORE_INPUT, new Object[]{this.toString(this.nextToken())});} else {return new SpelExpression(expressionString, ast, this.configuration);}}} catch (InternalParseException var6) {throw var6.getCause();}}}

三、其他设计模式

创建型模式
结构型模式

  • 1、设计模式——装饰器模式(Decorator Pattern)+ Spring相关源码

行为型模式

  • 1、设计模式——访问者模式(Visitor Pattern)+ Spring相关源码
  • 2、设计模式——中介者模式(Mediator Pattern)+ JDK相关源码
  • 3、设计模式——策略模式(Strategy Pattern)+ Spring相关源码
  • 4、设计模式——状态模式(State Pattern)
  • 5、设计模式——命令模式(Command Pattern)+ Spring相关源码
  • 6、设计模式——观察者模式(Observer Pattern)+ Spring相关源码
  • 7、设计模式——备忘录模式(Memento Pattern)
  • 8、设计模式——模板方法模式(Template Pattern)+ Spring相关源码
  • 9、设计模式——迭代器模式(Iterator Pattern)+ Spring相关源码
  • 10、设计模式——责任链模式(Chain of Responsibility Pattern)+ Spring相关源码
  • 11、设计模式——解释器模式(Interpreter Pattern)+ Spring相关源码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/134147.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

顶板事故防治vr实景交互体验提高操作人员安全防护技能水平

建筑业在我国各行业中属危险性较大且事故多发的行业&#xff0c;在建筑业“八大伤害”(高处坠落、坍塌、物体打击、触电、起重伤害、机械伤害、火灾爆炸及其他伤害)事故中&#xff0c;高处坠落事故的发生率最高、危险性极大。工地现场培训vr坠落体验利用虚拟现实技术还原各种情…

Day23力扣打卡

打卡记录 将 x 减到 0 的最小操作数&#xff08;逆向思维 滑动窗口&#xff09; 链接 将 x 减到 0 的最小操作数&#xff0c;可以逆向思考&#xff0c;求一个数组中的最大长度的滑动窗口&#xff0c;来使得这个窗口里的数等于 全数组之和 - x 的值。 class Solution { publ…

前后端交互—Express

Express 代码下载 Express 是基于 Node.js 平台&#xff0c;快速、开放、极简的 Web 开发框架。本质就是一个 npm 上的第三方包&#xff0c;提供了快速创建 Web 服务器的便捷方法。Express 的中文官网: https://www.expressjs.com.cn/ 最常见的两种服务器&#xff0c;分别是…

elasticsearch 基本使用,ES8.10

官方文档&#xff1a;https://www.elastic.co/guide/en/elasticsearch/reference/current/elasticsearch-intro.html ES版本&#xff1a;8.10 By default, Elasticsearch indexes all data in every field and each indexed field has a dedicated, optimized data structure…

LLM之幻觉(一):大语言模型幻觉解决方案综述

论文题目&#xff1a;《Cognitive Mirage: A Review of Hallucinations in Large Language Models》 ​论文链接&#xff1a;https://arxiv.org/abs/2309.06794v1 论文代码&#xff1a;https://github.com/hongbinye/cognitive-mirage-hallucinations-in-llms 一、幻觉介绍 …

原语:串并转换器

串并转换器OSERDESE2 可被Select IO IP核调用。 OSERDESE2允许DDR功能 参考&#xff1a; FPGA原语学习与整理第二弹&#xff0c;OSERDESE2串并转换器 - 知乎 (zhihu.com) 正点原子。 ISERDESE2原语和OSERDESE2原语是串并转换器&#xff0c;他的的功能都是实现串行数据和并行…

0基础学习VR全景平台篇第118篇:利用动作录制器功能避免重复操作 - PS教程

上课&#xff01;全体起立~ 大家好&#xff0c;欢迎观看蛙色官方系列全景摄影课程&#xff01; 嗨&#xff0c;大家好。欢迎收看蛙色VR系列教程之PS利用动作记录器节约补地时间。 大家拍摄在补地的时候&#xff0c;利用插件选择输入输出选项的时候&#xff0c;每次重复操作…

基于springboot垃圾分类管理系统

基于springboot垃圾分类管理系统 摘要 垃圾分类管理系统是一个基于现代技术和数据管理方法的解决方案&#xff0c;旨在协助城市和社区更有效地管理垃圾分类。在这个系统中&#xff0c;Spring Boot框架充当了后端应用程序的构建工具&#xff0c;为其提供了高度灵活的特性。该系统…

【设计模式】工厂模式总结

工厂模式 定义一个创建对象的接口&#xff0c;让子类决定实例化哪个类&#xff0c;而对象的创建统一交由工厂去生产。 工厂模式大致可以分为三类&#xff1a;简单工厂模式、工厂方法模式、抽象工厂模式。 简单工厂模式 简单工厂模式提供一个工厂类&#xff0c;根据传入的参…

MATLAB算法实战应用案例精讲-【图像处理】Transformer

目录 前言 算法原理 什么是transformer呢? self-attention 输入和位置编码 编码器 Softmax

我在Vscode学OpenCV 图像运算(权重、逻辑运算、掩码、位分解、数字水印)

文章目录 权重 _ 要求两幅图像是相同大小的。[ 1 ] 以数据说话&#xff08; 1&#xff09; 最终&#xff1a;&#xff08; 2 &#xff09;gamma _输出图像的标量值 [ 2 ] 图像的展现力gamma并不等同于增加曝光度&#xff08; 1 &#xff09;gamma100&#xff08; 2 &#xff09…

经典OJ题:链表中的倒数第K个节点

题目&#xff1a; 输入一个链表&#xff0c;输出该链表中倒数第k个结点。 题源&#xff1a;链表中倒数第k个结点_牛客题霸_牛客网 (nowcoder.com) 方法一&#xff1a;暴力求解法 可以线统计链表的节点个数&#xff0c;然后用链表节点的个数减去K&#xff0c;得出倒数第K个节点…

Jmeter全流程性能测试实战

项目背景&#xff1a; 我们的平台为全国某行业监控平台&#xff0c;经过3轮功能测试、接口测试后&#xff0c;98%的问题已经关闭&#xff0c;决定对省平台向全国平台上传数据的接口进行性能测试。 01、测试步骤 1、编写性能测试方案 由于我是刚进入此项目组不久&#xff0c…

异构融合计算技术白皮书(2023年)研读2

读到工业和信息化部电子第五研究所所做的《异构融合计算技术白皮书&#xff08;2023年&#xff09;》&#xff0c;我关注的重点是FPGA与异构计算。续前篇&#xff0c;前篇为第1和第2点。 3 异构计算技术困境 性能瓶颈&#xff0c;性能/灵活性矛盾&#xff0c;编程框架不统一 …

支付宝AI布局: 新产品助力小程序智能化,未来持续投入加速创新

支付宝是全球领先的独立第三方支付平台&#xff0c;致力于为广大用户提供安全快速的电子支付/网上支付/安全支付/手机支付体验&#xff0c;及转账收款/水电煤缴费/信用卡还款/AA收款等生活服务应用。 支付宝不仅是一个支付工具&#xff0c;也是一个数字生活平台&#xff0c;通过…

Ipswitch WS_FTP 12 安裝

Ipswitch WS.FTP.Professional.12.6.rar_免费高速下载|百度网盘-分享无限制 This works but quite difficult to figure out. It didnt allow me to replace the wsftpext.dll at 1st and had to test lots of ways how to replace it. This is how I did: 1. Follow the instr…

JS逆向爬虫---请求参数加密③【比特币交易爬虫】

查询参数确定 t无加密 请求头参数加密 X-Apikey参数加密确定 X-Apikey逆向 const API_KEY "a2c903cc-b31e-4547-9299-b6d07b7631ab" function encryptApiKey(){ var t API_KEY, e t.split(""), n e.splice(0, 8);return t e.concat(n).join("&…

Oracle RAC是啥?

Oracle RAC&#xff0c;全称是Oracle Real Application Cluster&#xff0c;翻译过来为Oracle真正的应用集群&#xff0c;它是Oracle提供的一个并行集群系统&#xff0c;由 Oracle Clusterware&#xff08;集群就绪软件&#xff09; 和 Real Application Cluster&#xff08;RA…

ESP32网络开发实例-Web服务器以仪表形式显示传感器计数

Web服务器以仪表形式显示传感器计数 文章目录 Web服务器以仪表形式显示传感器计数1、应用介绍2、软件准备3、硬件准备4、代码实现4.1 Web页面文件4.2 Web服务器代码实现在本文中,我们将介绍使用服务器发送事件 (SSE) 构建 ESP32 仪表 Web 服务器。服务器将自动向所有连接的网络…

游戏开发中的“御用中介“

点击上方亿元程序员关注和★星标 引言 大家好&#xff0c;我是亿元程序员&#xff0c;一位有着8年游戏行业经验的主程。 本系列是《和8年游戏主程一起学习设计模式》&#xff0c;让糟糕的代码在潜移默化中升华&#xff0c;欢迎大家关注分享收藏订阅。 游戏开发中的"御用…