在阎宏博士的《JAVA与模式》一书中开头是这样描述解释器(Interpreter)模式的:

  解释器模式是类的行为模式。给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器。客户端可以使用这个解释器来解释这个语言中的句子。

解释器模式的结构

  下面就以一个示意性的系统为例,讨论解释器模式的结构。系统的结构图如下所示:

  模式所涉及的角色如下所示:

  (1)抽象表达式(Expression)角色:声明一个所有的具体表达式角色都需要实现的抽象接口。这个接口主要是一个interpret()方法,称做解释操作。

  (2)终结符表达式(Terminal Expression)角色:实现了抽象表达式角色所要求的接口,主要是一个interpret()方法;文法中的每一个终结符都有一个具体终结表达式与之相对应。比如有一个简单的公式R=R1+R2,在里面R1和R2就是终结符,对应的解析R1和R2的解释器就是终结符表达式。

  (3)非终结符表达式(Nonterminal Expression)角色:文法中的每一条规则都需要一个具体的非终结符表达式,非终结符表达式一般是文法中的运算符或者其他关键字,比如公式R=R1+R2中,“+"就是非终结符,解析“+”的解释器就是一个非终结符表达式。

  (4)环境(Context)角色:这个角色的任务一般是用来存放文法中各个终结符所对应的具体值,比如R=R1+R2,我们给R1赋值100,给R2赋值200。这些信息需要存放到环境角色中,很多情况下我们使用Map来充当环境角色就足够了。

  为了说明解释器模式的实现办法,这里给出一个最简单的文法和对应的解释器模式的实现,这就是模拟Java语言中对布尔表达式进行操作和求值。

  在这个语言中终结符是布尔变量,也就是常量true和false。非终结符表达式包含运算符and,or和not等布尔表达式。这个简单的文法如下:

    Expression  ::= Constant | Variable | Or | And | Not

    And     ::= Expression 'AND' Expression

    Or     ::= Expression 'OR' Expression

    Not     ::= 'NOT' Expression

    Variable  ::= 任何标识符

    Constant    ::= 'true' | 'false'

  解释器模式的结构图如下所示:

  

  源代码

  抽象表达式角色

public abstract class Expression {
/**
* 以环境为准,本方法解释给定的任何一个表达式
*/
public abstract boolean interpret(Context ctx);
/**
* 检验两个表达式在结构上是否相同
*/
public abstract boolean equals(Object obj);
/**
* 返回表达式的hash code
*/
public abstract int hashCode();
/**
* 将表达式转换成字符串
*/
public abstract String toString();
}

  一个Constant对象代表一个布尔常量

public class Constant extends Expression{

    private boolean value;

    public Constant(boolean value){
this.value = value;
} @Override
public boolean equals(Object obj) { if(obj != null && obj instanceof Constant){
return this.value == ((Constant)obj).value;
}
return false;
} @Override
public int hashCode() {
return this.toString().hashCode();
} @Override
public boolean interpret(Context ctx) { return value;
} @Override
public String toString() {
return new Boolean(value).toString();
} }

  一个Variable对象代表一个有名变量

public class Variable extends Expression {

    private String name;

    public Variable(String name){
this.name = name;
}
@Override
public boolean equals(Object obj) { if(obj != null && obj instanceof Variable)
{
return this.name.equals(
((Variable)obj).name);
}
return false;
} @Override
public int hashCode() {
return this.toString().hashCode();
} @Override
public String toString() {
return name;
} @Override
public boolean interpret(Context ctx) {
return ctx.lookup(this);
} }

  代表逻辑“与”操作的And类,表示由两个布尔表达式通过逻辑“与”操作给出一个新的布尔表达式的操作

public class And extends Expression {

    private Expression left,right;

    public And(Expression left , Expression right){
this.left = left;
this.right = right;
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof And)
{
return left.equals(((And)obj).left) &&
right.equals(((And)obj).right);
}
return false;
} @Override
public int hashCode() {
return this.toString().hashCode();
} @Override
public boolean interpret(Context ctx) { return left.interpret(ctx) && right.interpret(ctx);
} @Override
public String toString() {
return "(" + left.toString() + " AND " + right.toString() + ")";
} }

  代表逻辑“或”操作的Or类,代表由两个布尔表达式通过逻辑“或”操作给出一个新的布尔表达式的操作

public class Or extends Expression {
private Expression left,right; public Or(Expression left , Expression right){
this.left = left;
this.right = right;
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof Or)
{
return this.left.equals(((Or)obj).left) && this.right.equals(((Or)obj).right);
}
return false;
} @Override
public int hashCode() {
return this.toString().hashCode();
} @Override
public boolean interpret(Context ctx) {
return left.interpret(ctx) || right.interpret(ctx);
} @Override
public String toString() {
return "(" + left.toString() + " OR " + right.toString() + ")";
} }

  代表逻辑“非”操作的Not类,代表由一个布尔表达式通过逻辑“非”操作给出一个新的布尔表达式的操作

public class Not extends Expression {

    private Expression exp;

    public Not(Expression exp){
this.exp = exp;
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof Not)
{
return exp.equals(
((Not)obj).exp);
}
return false;
} @Override
public int hashCode() {
return this.toString().hashCode();
} @Override
public boolean interpret(Context ctx) {
return !exp.interpret(ctx);
} @Override
public String toString() {
return "(Not " + exp.toString() + ")";
} }

  环境(Context)类定义出从变量到布尔值的一个映射

public class Context {

    private Map<Variable,Boolean> map = new HashMap<Variable,Boolean>();

    public void assign(Variable var , boolean value){
map.put(var, new Boolean(value));
} public boolean lookup(Variable var) throws IllegalArgumentException{
Boolean value = map.get(var);
if(value == null){
throw new IllegalArgumentException();
}
return value.booleanValue();
}
}

  客户端类

public class Client {

    public static void main(String[] args) {
Context ctx = new Context();
Variable x = new Variable("x");
Variable y = new Variable("y");
Constant c = new Constant(true);
ctx.assign(x, false);
ctx.assign(y, true); Expression exp = new Or(new And(c,x) , new And(y,new Not(x)));
System.out.println("x=" + x.interpret(ctx));
System.out.println("y=" + y.interpret(ctx));
System.out.println(exp.toString() + "=" + exp.interpret(ctx));
} }

  运行结果如下:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABD8AAACGCAIAAABVIba3AAAgAElEQVR4nO3da3ATV4IvcH2euXe+bNVu1dRWpbZmazcJVVOTyZg745nN1K07m3szW8OEjDOQF9E4HsckGR52cJQAwQEi24QxCiygQGKbTIKFhYONQRgcgonNM8Q2D2dInGCbp1+y22+9+344Vut09zmtblmyJPv/K1UiWt2nT58+ap1/d0s2CXIdHR2VlZWVlZXC/DPnN3zOb2BspD4vzieVYR0dHcneA8mBtwMAAEAq4w1UTPQ/yDBuFmuVQub8hs/5DYwN6fPJjhLJNM9jDN4XAACJVl1dvXz58izjli9fXl1dHcdCIB2phyiR9ILoMofN+Q2MDenzIQiFQqHQvM0weHcAQLrbvHnzxMREsmvB5nA4srOzy8rK3jeurKzsqaeecjgccSm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/f1r167dsGHDvn37GhsbkV4AAAAAAECNpBeLxVJZWdnQ0HDixImBgYHkXHvZvn17dXV1U1MT0gsAAAAAAKiR9LJx40aXy3Xx4sXkXHuRvvdSVVXV0tKC9AIAAAAAAGokvezevbu5uTlp33uhf3Ps1KlTSC8AAAAAAKBG0kt1dXWSf3OMDjD4ey8AAAAAAKA2lDp/70VbUloHAAAAAABShyIjzFJ6GQYAAAAAAJgZOrokML2MGBdzJQAAAAAAIE1FjQl0dElUeuk3rg8AAAAAAOaZGIJD/NMLAAAAAABASkF6AQAAAACA9CCllf8PSTkPHzB0/NsAAAAASUVORK5CYII=" alt="" />

设计模式《JAVA与模式》之解释器模式的更多相关文章

  1. 设计模式之第5章-解释器模式(Java实现)

    设计模式之第5章-解释器模式(Java实现) “开个商店好麻烦,做个收单的系统,发现类的方法好多.”“真是的,不就是简单的四则运算,这都不会!”你说你会啊.来来来,你把以下的方法用代码写出来: a+b ...

  2. C#设计模式学习笔记:(23)解释器模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8242238.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第十一个模式-- ...

  3. 行为型模式(十一) 解释器模式(Interpreter)

    一.动机(Motivate) 在软件构建过程中,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变化.在这种情况下,将特定领域的问题表达为某种语法规 ...

  4. 我所理解的设计模式(C++实现)——解释器模式(Interpreter Pattern)

    概述: 未来机器智能化已然成为趋势,现在手机都能听懂英语和普通话,那我大中华几万种方言的被智能化也许也是趋势,我们的方言虽然和普通话相似,但是还是不一样的.这可能需要一个新的语法分析器来帮助我们. 我 ...

  5. 【设计模式】行为型11解释器模式(Interpreter Pattern)

      解释器模式(Interpreter Pattern) 解释器模式应用场景比较小,也没有固定的DEMO,中心思想就是自定义解释器用来解释固定的对象. 定义:给定一个语言,定义它的文法表示,并定义一个 ...

  6. 设计模式(二十一)——解释器模式(Spring 框架中SpelExpressionParser源码分析)

    1 四则运算问题 通过解释器模式来实现四则运算,如计算 a+b-c 的值,具体要求 1) 先输入表达式的形式,比如 a+b+c-d+e,  要求表达式的字母不能重复 2) 在分别输入 a ,b, c, ...

  7. 设计模式22---设计模式之解释器模式(Interpreter)(行为型)

    1.讲解解释器模式 1.1解释器模式定义 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 1.2解释器模式要点 解析器:把描述客户端调用要求的表达式, ...

  8. [Python设计模式] 第27章 正则表达式——解释器模式

    github地址:https://github.com/cheesezh/python_design_patterns 解释器模式 解释器模式,给定一个语言,定一个它的文法的一种表示,并定一个一个解释 ...

  9. [19/04/30-星期二] GOF23_行为型模式(中介者模式、命令模式、解释器模式、访问者模式)

    一.中介者模式(meditor) [中介] /*** * 抽象中介者接口和其具体实现类"经理"类 */ package cn.sxt.meditor; import java.ut ...

  10. Java设计模式(24)——行为模式之解释器模式(Interpreter)

    一.概述 概念 自己定义文法,实际中还是很少出现的,作了解 给出一篇网友的参考博文:http://blog.csdn.net/ylchou/article/details/7594135

随机推荐

  1. MVCS框架的注意点

    1.Service最好用一个接口对应一个service(便于增加扩展方法) 2.除Services和Model外都需继承自各自对应的父类 3.View不要轻易重写Start和Awake方法(含与启动有 ...

  2. css初识和css选择器

    一.css是什么 css(cascading style sheet)定义如何显示HTML元素,给HTML设置样式,显得更为美观. 二.css的引入方式 1.行内引入 在标签中添加一个style是属性 ...

  3. 860. Lemonade Change

    class Solution { public: bool lemonadeChange(vector<int>& bills) { , ten = ; for (int i : ...

  4. Docker技术入门与实战(文摘)

    第一部分 基础入门 第1章 初识容器与Docker 第2章 核心概念与安装配置 第二部分 实战案例 第三部分 进阶技能 第四部分 开源项目

  5. react属性绑定

    1.属性值绑定state里的数据,不用引号 class App extends Component { constructor(props){ super(props); this.state = { ...

  6. Codeforces Round #543 (Div. 2)B,C

    https://codeforces.com/contest/1121 B 题意 给你n(<=1000)个数ai,找出最多对和相等的数,每个数只能用一次,且每个数保证各不相同 题解 重点:每个数 ...

  7. Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)

    https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...

  8. 引用限定符(c++11)

    1.概念 1)下面这种情况将对一个右值调用成员函数.对右值赋值 string s1 = "abc", s2 = "def"; auto n = (s1 + s2 ...

  9. Win7 IIS 部署站点遇到的问题 如 HTTP 错误 404.XX

    HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面. 解决办法:设置为允许-面相 =============== ...

  10. ArcGIS API for Silverlight部署本地地图服务

    这一节我们来讲新建立的ArcGIS API for Silverlight应用程序如何加载自己的地图服务的问题,网上的资料讲的都有点含糊不清,这次我们详细的讲一下配置的步骤: 首先介绍下我们的开发和部 ...