java设计模式适配器模式

Interpreter design pattern is one of the behavioral design pattern. Interpreter pattern is used to defines a grammatical representation for a language and provides an interpreter to deal with this grammar.

口译设计模式是行为设计模式之一 。 解释器模式用于定义一种语言的语法表示形式,并提供解释器来处理这种语法。

口译员设计模式 (Interpreter Design Pattern)

The best example of interpreter design pattern is java compiler that interprets the java source code into byte code that is understandable by JVM. Google Translator is also an example of interpreter pattern where the input can be in any language and we can get the output interpreted in another language.

解释器设计模式的最佳示例是Java编译器,它将Java源代码解释为JVM可以理解的字节代码。 Google Translator也是解释器模式的一个示例,其中输入可以使用任何语言,而我们可以使用另一种语言来解释输出。

解释器模式示例 (Interpreter Pattern Example)

To implement interpreter pattern, we need to create Interpreter context engine that will do the interpretation work.

为了实现解释器模式,我们需要创建解释器上下文引擎来完成解释工作。

Then we need to create different Expression implementations that will consume the functionalities provided by the interpreter context.

然后,我们需要创建不同的Expression实现,这些实现将使用解释器上下文提供的功能。

Finally we need to create the client that will take the input from user and decide which Expression to use and then generate output for the user.

最后,我们需要创建一个客户端,它将从用户那里获取输入,并确定要使用哪个Expression,然后为用户生成输出。

Let’s understand this with an example where the user input will be of two forms – “<Number> in Binary” or “<Number> in Hexadecimal.” Our interpreter client should return it in format “<Number> in Binary= <Number_Binary_String>” and “<Number> in Hexadecimal= <Number_Binary_String>” respectively.

让我们以一个示例来了解这一点,其中用户输入将采用两种形式–“ <Number> in Binary ”或“ <Number> in Hexadecimal 。” 我们的解释器客户端应分别以“ <Number> in Binary= <Number_Binary_String> <Number> in Hexadecimal= <Number_Binary_String> ”格式返回它。

Our first step will be to write the Interpreter context class that will do the actual interpretation.

我们的第一步将是编写解释器上下文类,该类将进行实际的解释。

package com.journaldev.design.interpreter;public class InterpreterContext {public String getBinaryFormat(int i){return Integer.toBinaryString(i);}public String getHexadecimalFormat(int i){return Integer.toHexString(i);}
}

Now we need to create different types of Expressions that will consume the interpreter context class.

现在,我们需要创建不同类型的表达式,这些表达式将使用解释器上下文类。

package com.journaldev.design.interpreter;public interface Expression {String interpret(InterpreterContext ic);
}

We will have two expression implementations, one to convert int to binary and other to convert int to hexadecimal format.

我们将有两种表达式实现,一种将int转换为二进制,另一种将int转换为十六进制格式。

package com.journaldev.design.interpreter;public class IntToBinaryExpression implements Expression {private int i;public IntToBinaryExpression(int c){this.i=c;}@Overridepublic String interpret(InterpreterContext ic) {return ic.getBinaryFormat(this.i);}}
package com.journaldev.design.interpreter;public class IntToHexExpression implements Expression {private int i;public IntToHexExpression(int c){this.i=c;}@Overridepublic String interpret(InterpreterContext ic) {return ic.getHexadecimalFormat(i);}}

Now we can create our client application that will have the logic to parse the user input and pass it to correct expression and then use the output to generate the user response.

现在,我们可以创建客户端应用程序,该应用程序将具有逻辑来解析用户输入并将其传递给正确的表达式,然后使用输出生成用户响应。

package com.journaldev.design.interpreter;public class InterpreterClient {public InterpreterContext ic;public InterpreterClient(InterpreterContext i){this.ic=i;}public String interpret(String str){Expression exp = null;//create rules for expressionsif(str.contains("Hexadecimal")){exp=new IntToHexExpression(Integer.parseInt(str.substring(0,str.indexOf(" "))));}else if(str.contains("Binary")){exp=new IntToBinaryExpression(Integer.parseInt(str.substring(0,str.indexOf(" "))));}else return str;return exp.interpret(ic);}public static void main(String args[]){String str1 = "28 in Binary";String str2 = "28 in Hexadecimal";InterpreterClient ec = new InterpreterClient(new InterpreterContext());System.out.println(str1+"= "+ec.interpret(str1));System.out.println(str2+"= "+ec.interpret(str2));}
}

The client also has a main method for testing purpose, when we run above we get following output:

客户端还有一个主要的测试方法,当我们在上面运行时,得到以下输出:

28 in Binary= 11100
28 in Hexadecimal= 1c

口译员设计模式示例类图 (Interpreter Design Pattern Example Class Diagram)

关于解释器模式的要点 (Important Points about Interpreter pattern)

  1. Interpreter pattern can be used when we can create a syntax tree for the grammar we have.当我们可以为已有的语法创建语法树时,可以使用解释器模式。
  2. Interpreter design pattern requires a lot of error checking and a lot of expressions and code to evaluate them. It gets complicated when the grammar becomes more complicated and hence hard to maintain and provide efficiency.解释器设计模式需要进行大量的错误检查,并需要大量的表达式和代码来对其进行评估。 当语法变得更加复杂,因此难以维护和提供效率时,它将变得复杂。
  3. java.util.Pattern and subclasses of java.text.Format are some of the examples of interpreter pattern used in JDK.java.util.Patternjava.text.Format子类是JDK中使用的解释器模式的一些示例。

翻译自: https://www.journaldev.com/1635/interpreter-design-pattern-java

java设计模式适配器模式

java设计模式适配器模式_Java解释器设计模式相关推荐

  1. java设计模式适配器模式_Java中的适配器设计模式

    java设计模式适配器模式 适配器设计模式是一种结构设计模式 ,可以帮助我们连接到通过不同接口公开相似功能的旧版或第三方代码. 适配器的现实世界是我们用来将USB电缆连接到以太网端口的类比. 在设计一 ...

  2. 设计模式适配器模式_适配器设计模式示例

    设计模式适配器模式 本文是我们名为" Java设计模式 "的学院课程的一部分. 在本课程中,您将深入研究大量的设计模式,并了解如何在Java中实现和利用它们. 您将了解模式如此重要 ...

  3. java 接口 设计模式吗_JAVA接口设计模式-工厂模式

    当更换使用的IFruit子类的时候主方法没有任何的变化就可以实现子类的变更,这样的设计就是工厂设计模式. package com.gwolf.springmvc.factory; interface ...

  4. java 设计模式 示例_Java设计模式–示例教程

    java 设计模式 示例 Design Patterns are very popular among software developers. A design pattern is a well- ...

  5. Java | 设计模式-适配器模式

    继代理模式后又来到适配器模式啦,想看之前的也有哦.持续更新中哦.让我们一起加油吧兄弟们,干他. 很喜欢一句话:"八小时内谋生活,八小时外谋发展". 你好,如果喜欢,请一起坚持!! ...

  6. java设计模式教程_Java设计模式教程

    java设计模式教程 课程大纲 架构和计算机科学中的设计模式是记录特定专业领域中设计问题的解决方案的正式方法. 这个想法是由建筑师Christopher Alexander在建筑领域引入的,并已被修改 ...

  7. Java设计模式学习记录-解释器模式

    前言 这次介绍另一个行为模式,解释器模式,都说解释器模式用的少,其实只是我们在日常的开发中用的少,但是一些开源框架中还是能见到它的影子,例如:spring的spEL表达式在解析时就用到了解释器模式,以 ...

  8. JAVA设计模式--适配器模式

    目录 1.什么是适配器模式 2.适配器模式的适用场景 3.适配器模式的结构 4.适配器模式应用举例 5.适配器模式的选择 参考文章 1.什么是适配器模式 适配器(Adapter)模式又叫做包装( Wr ...

  9. Java代码审计-设计模式-适配器模式

    Java设计模式-适配器模式(Adapter Pattern) 目录 什么是适配器模式 适配器模式的3种类型 JavaSE适配器模式的应用 Struts2适配器模式的应用 适配器模式是一种" ...

最新文章

  1. Linux命令- echo、grep 、重定向、1>2、2>1的介绍
  2. 微电网日前优化调度 。算例有代码(0)
  3. 专业计算机能力考试 技巧,全国专业技术人员计算机应用能力考试应试技巧
  4. 解释为脑瘫的那张图_Python GIL全局解释器锁详解(深度剖析)
  5. pytorch构造可迭代的DataLoader,动态流式读取数据源,不担心内存炸裂(pytorch Data学习三)
  6. POJ1742Coins
  7. mysql 数据库基础教程(一)
  8. 【项目源码】JavaWeb网上购书系统
  9. 元胞自动机:森林火灾模拟(Python:numpy、seaborn)
  10. 沟通的艺术I:什么是沟通
  11. 控制极限(UCL,LCL) 和规格极限(USL,LSL)
  12. 使用vue-admin-template基础模板开发后台管理系统必会技能
  13. 2021 Android APK反编译 apktool使用教程
  14. jQuery(简介、特点、使用方法、【重点】jQuery的选择器:是jQuery的灵魂、jQuery的属性:操作标签的属性)
  15. pico的学习之路(四)——HC-SR501人体感应模块(树莓派pico实现)
  16. html初学者对相对地址,绝对地址的理解
  17. C#操作开机自启动(写进注册表)
  18. yolov5——detect.py代码【注释、详解、使用教程】
  19. sumatrapdf关闭当前tab快捷键
  20. Arduino PID Autotune Library

热门文章

  1. cxGrid 怎样才能让不自动换行 WordWrap:=false
  2. DbEntry 访问Access2010数据库
  3. WCF开发实战系列四:使用Windows服务发布WCF服务
  4. [转载] Python3 字典 values() 方法
  5. docker入门与部署微服务--学习笔记
  6. Echo团队Alpha冲刺随笔 - 第六天
  7. MYSQL:RELPACE用法
  8. 利用ajaxSubmit()方法实现Form提交表单后回调
  9. App 更换应用图标
  10. Git基础教程(三)