首页 > 编程语言 >Java: Interpreter Pattern

Java: Interpreter Pattern

时间:2022-10-01 18:22:06浏览次数:46  
标签:12 Java Pattern 09 ctx return 2022 Interpreter public

 

/**
 * 版权所有 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();



}

  

/**
 * 版权所有 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();
    }
}

  

/**
 * 版权所有 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;
    }

}

  

/**
 * 版权所有 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();
    }
}

  

/**
 * 版权所有 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() + ")";
    }
}

  

/**
 * 版权所有 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() + ")";
    }
}

  

/**
 * 版权所有 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() + ")";
    }
}

  

调用:

            //解释器模式
            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));

  

输出:

x = false
y = true
((true AND x) OR (y AND  (Not x))) = true

  

 

标签:12,Java,Pattern,09,ctx,return,2022,Interpreter,public
From: https://www.cnblogs.com/geovindu/p/16747548.html

相关文章

  • Javaweb学习笔记第十四弹---对于Cookie和Filter的学习
    ApacheTomcat-TomcatNativeDownloads会话追踪技术会话:打开浏览器,建立连接,直到一方断开连接,会话才会结束;在一次会议中,可以有多次请求。会话追踪:在多次请求间,共享数......
  • HelloWorld和Java程序运行机制
    HelloWorld1.新建java文件后缀名为.java2.编写代码publicclassHello{publicstaticvoidmain(String[]args){System.out.print("Hello,World!");......
  • 给我的Java随笔做个说明
    本说明会逐渐更新2022-10-1我的java笔记目前是根据狂神说Java的视频写的笔记,后面会慢慢加东西,都会在这个里面说明。另外:一部分笔记和大多数图片是我从其他人的笔记抄过......
  • Java(15)Object类
    前言Object类是Java中所有类的始祖,在Java中每个类都扩展了Object。如果没有明确地指出超类,Object就被认为是这个类的超类。由于在Java中每个类都是由Object类扩展而来的,所......
  • Java中队列和链表性能对比-ArrayList和LinkedList
    本文使用ArrayList和LinkedList,分别对比了队列链表的add,get的性能。 具体代码如下,可以直接运行importjava.util.ArrayList;importjava.util.LinkedList;importja......
  • Java 集合框架之Collection,一文解决
     JDK提供了一些特殊的类,这些类可以存储任意类型的对象,并且长度可变,在Java中这些被称为集合。按照存储结构可以分为两大类,单列集合Collection和双列集合常用集合类如下......
  • 力扣209(java)-单词规律(简单)
    题目:给定一种规律pattern 和一个字符串 s ,判断s 是否遵循相同的规律。这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 s 中的每个非空单词之间存......
  • Java泛型中<T> T 与 T的区别和用法
    有的方法返回值是<T>T,有的是T,区别在哪里?下面是一个泛型方法,<T>声明此方法有一个泛型T,也可以理解声明一个泛型方法.    下面三个T,第一个T表示是泛型,第二个......
  • 肖sir___第二个月Javascript_04
    1.1JavaScript简介JavaScript是互联网上最流行的脚本语言,这门语言可用于HTML和web,更可广泛用于服务器、PC、笔记本电脑、平板电脑和智能手机等设备JavaScript是......
  • 肖sir___第二个月java操作JDBC_02
    1.1JDBC概述JDBC(JavaDataBaseConnectivity)是Java和数据库之间的一个桥梁,是一个规范而不是一个实现,能够执行SQL语句。它由一组用Java语言编写的类和接口组成。各种不同......