Java: Interpreter Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 解释器模式 Interpreter Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Expression.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.interpreter; /** * * * */ public abstract class Expression { /** * Given a BooleanExp object denoting a term, * this method interprets this term relative to a Context * object. */ public abstract boolean interpret(Context ctx); /** * Given a BooleanExp object denoting a term, * this method test whether the given argument * denoting another term is structurally the same. */ public abstract boolean equals(Object o); /** * Returns a hash code of this term. */ public abstract int hashCode(); /** * Converts a term into a string. Can be used as the * basis for calculating the hashCode. */ public abstract String toString(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 解释器模式 Interpreter Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Context.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.interpreter; import java.util.HashMap; /** * * * */ public class Context { /** * * * */ private HashMap map = new HashMap(); /** * * * */ public void assign(Variable var, boolean value) { map.put(var, new Boolean(value)); } /** * * * */ public boolean lookup(Variable var) throws IllegalArgumentException { Boolean value = (Boolean) map.get(var); if (value == null ) { throw new IllegalArgumentException(); } return value.booleanValue(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 解释器模式 Interpreter Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Variable.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.interpreter; /** * * * */ public class Variable extends Expression{ /** * * * */ private String name; /** * * * */ public Variable(String name) { this .name = name; } /** * * * */ public boolean interpret(Context ctx) { return ctx.lookup( this ); } /** * * * */ public boolean equals(Object o) { if (o != null && o instanceof Variable) { return this .name.equals(((Variable) o).name); } return false ; } /** * * * */ public int hashCode() { return ( this .toString()).hashCode(); } /** * * * */ public String toString() { return name; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 解释器模式 Interpreter Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Constant.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.interpreter; /** * * * */ public class Constant extends Expression{ /** * * * */ private boolean value; /** * * * */ public Constant( boolean value) { this .value = value; } /** * * * */ public boolean interpret(Context ctx) { return value; } /** * * * */ public boolean equals(Object o) { if (o != null && o instanceof Constant) { return this .value == ((Constant) o).value; } return false ; } /** * * * */ public int hashCode() { return ( this .toString()).hashCode(); } /** * * * */ public String toString() { return new Boolean(value).toString(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 解释器模式 Interpreter Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuAnd.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.interpreter; /** * * * */ public class DuAnd extends Expression{ /** * * * */ private Expression left, right; /** * * * */ public DuAnd(Expression left, Expression right) { this .left = left; this .right = right; } /** * * * */ public boolean interpret(Context ctx) { return left.interpret(ctx) && right.interpret(ctx); } /** * * * */ public boolean equals(Object o) { if (o != null && o instanceof DuAnd) { return this .left.equals(((DuAnd) o).left) && this .right.equals(((DuAnd) o).right); } return false ; } /** * * * */ public int hashCode() { return ( this .toString()).hashCode(); } /** * * * */ public String toString() { return "(" + left.toString() + " AND " + right.toString() + ")" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 解释器模式 Interpreter Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuNot.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.interpreter; /** * * * */ public class DuNot extends Expression{ /** * * * */ private Expression exp; /** * * * */ public DuNot(Expression exp) { this .exp = exp; } /** * * * */ public boolean interpret(Context ctx) { return !exp.interpret(ctx); } /** * * * */ public boolean equals(Object o) { if (o != null && o instanceof DuNot) { return this .exp.equals(((DuNot) o).exp); } return false ; } /** * * * */ public int hashCode() { return ( this .toString()).hashCode(); } /** * * * */ public String toString() { return " (Not " + exp.toString() + ")" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 解释器模式 Interpreter Pattern * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuOr.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.interpreter; /** * * * */ public class DuOr extends Expression{ /** * * * */ private Expression left, right; /** * * * */ public DuOr(Expression left, Expression right) { this .left = left; this .right = right; } /** * * * */ public boolean interpret(Context ctx) { return left.interpret(ctx) || right.interpret(ctx); } /** * * * */ public boolean equals(Object o) { if (o != null && o instanceof DuOr) { return this .left.equals(((DuOr) o).left) && this .right.equals(((DuOr) o).right); } return false ; } /** * * * */ public int hashCode() { return ( this .toString()).hashCode(); } /** * * * */ public String toString() { return "(" + left.toString() + " OR " + right.toString() + ")" ; } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 | //解释器模式 Context ctx; Expression exp ; ctx = new Context(); Variable x = new Variable( "x" ); Variable y = new Variable( "y" ); Constant ducc = new Constant( true ); ctx.assign(x, false ); ctx.assign(y, true ); exp = new DuOr( new DuAnd(ducc, x) , new DuAnd(y, new DuNot(x))); System.out.println( "x = " + x.interpret(ctx)); System.out.println( "y = " + y.interpret(ctx)); System.out.println( exp.toString() + " = " + exp.interpret(ctx)); |
输出:
1 2 3 | x = false y = true (( true AND x) OR (y AND (Not x))) = true |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!