Java: Interpreter Pattern

 

/**
 * 版權所有 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

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章