《JAVA设计模式》之解释器模式(Interpreter)
在阎宏博士的《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+Q2MDenzIQiFQqHQvM0weHcAQLrbvHnzxMREsmvB5nA4srOzy8rK3jeurKzsqaeecjgccSmE1CeV2wp4FOOT6fSiiC6VdnOlbWml3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eLxVJZWdnQ0HDixImBgYHkXHvZvn17dXV1U1MT0gsAAAAAAKiR9LJx40aXy3Xx4sXkXHuRvvdSVVXV0tKC9AIAAAAAAGokvezevbu5uTlp33uhf3Ps1KlTSC8AAAAAAKBG0kt1dXWSf3OMDjD4ey8AAAAAAKA2lDp/70VbUloHAAAAAABShyIjzFJ6GQYAAAAAAJgZOrokML2MGBdzJQAAAAAAIE1FjQl0dElUeuk3rg8AAAAAAOaZGIJD/NMLAAAAAABASkF6AQAAAACA9CCllf8PSTkPHzB0/NsAAAAASUVORK5CYII=" alt="" />
原文地址:《JAVA与模式》之解释器模式
《JAVA设计模式》之解释器模式(Interpreter)的更多相关文章
- 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)
原文:乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) 作 ...
- C#设计模式:解释器模式(Interpreter Pattern)
一,C#设计模式:解释器模式(Interpreter Pattern) 1,解释器模式的应用场合是Interpreter模式应用中的难点,只有满足“业务规则频繁变化,且类似的模式不断重复出现,并且容易 ...
- 二十四种设计模式:解释器模式(Interpreter Pattern)
解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...
- 折腾Java设计模式之解释器模式
解释器模式 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 意图 给定一个语言,定义它的文法表 ...
- JAVA设计模式之解释器模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述解释器(Interpreter)模式的: 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个 ...
- [设计模式] 15 解释器模式 Interpreter
在GOF的<设计模式:可复用面向对象软件的基础>一书中对解释器模式是这样说的:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子.如果一种特定类 ...
- 设计模式之解释器模式(Interpreter)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 20.java设计模式之解释器模式
基本需求 实现四则运算,如计算a+b-c+d的值 先输入表达式的形式,如a+b-c+d,要求表达式正确 再分别输出a,b,c,d的值 最后求出结果 传统方案 编写一个方法,接收表达式的形式,根据用户输 ...
- 设计模式 笔记 解释器模式 Interpreter
//---------------------------15/04/26---------------------------- //Interpreter 解释器模式----类行为型模式 /* 1 ...
- 行为型设计模式之解释器模式(Interpreter)
结构 意图 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 适用性 当有一个语言需要解释执行, 并且你可将该语言中的句子表示为一个抽象语法树时,可使用 ...
随机推荐
- js的validate插件异步效验
js代码 $(function () { $("#regForm").validate({ onsubmit:true,// 是否在提交是验证 onkeyup: false, // ...
- echart 属性设计大全
// 全图默认背景 // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87cefa','#da70d6','#32cd ...
- 简述vue中v-if和v-show的区别
vue中的 v-if 和 v-show 二者都可以动态的控制元素的隐藏和显示,但是他们控制的原理是不同的 v-if v-if 控制元素显示或隐藏是把dom元素整个的渲染或者删除,如果删除,也就是页面中 ...
- meter标签度量衡如何改变颜色
此文章为转载,目的为了方便整理学习笔记. 在meter中要想改变颜色,需要用到五个值,分别是:min(最小值).max(最大值).low.high.value和optimum,其中前四个值会把整个进度 ...
- python基础--局部变量与全局变量
#全局变量作用于全局或整个程序中,程序执行完毕后销毁,局部变量作用在当前函数中,调用函数执行完毕及销毁 #如果函数的内容无global关键字,优先读取同名局部变量,如果没有同名局部变量,只能读取同名全 ...
- 【改】shell 判断文件中有无特定子串方法(grep)
转自:https://blog.csdn.net/zhuguiqin1/article/details/79160923 利用grep执行的命令结束代码$?的值来判断是否已经grep到特定的值. 当$ ...
- mysql +keeplive+drbd高可用架构(MHA基于监听端口VIP的高可用)
1MySQL+DRBD+keepalived高可用架构 DRBD(DistributedReplicatedBlockDevice)是一个基于块设备级别在远程服务器直接同步和镜像数据的开源软件,类似于 ...
- alert(1) to win
一. function escape(s) { return '<script>console.log("'+s+'");</script>'; } 两种思 ...
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
- Oracle12c创建及删除PDB
在12C R1版本中只支持Global Shared Undo模式,所有container共享一个UNDO表空间:在12C R2引入了PDB Local Undo模式,每个container都有自己的 ...