Design Patterns Java - Interpreter Php

1) What have we done?

In order to make system interpreting expressions we need to little rethink about following expression:

(a+b)*4+2*c

what elements we can find in our expression:

a, b, c - Variable
4,2 - Constant
+ - Add (two arguments, both Vectors)
* - Multiply(two arguments, one Vectors, second Scalar)

And here we apply "Interpreter" first of this pattern tells us to create exactly class like these elements:
- Variable
- Constant
- Add
- Multiply

All will implements "Expression" interface which has a method "evaluate" for interpret given expression

import java.util.Map;

public interface Expression
{
    public Object evaluate(Map<String, Vector> map);
}

import java.util.List;
import java.util.Map;

public class Variable implements Expression
{
    String letter;

    public Variable(String letter)
    {
        this.letter = letter;
    }

    @Override
    public Object evaluate(Map<String, Vector> list)
    {
        return list.get(letter);
    }
}

import java.util.Map;

public class Constant implements Expression
{
    double value;

    public Constant(double value)
    {
        this.value = value;
    }

    @Override
    public Object evaluate(Map<String, Vector> map)
    {
        return value;
    }
}


import java.util.Map;

public class Add implements Expression
{
    private Expression expressionLeft;

    private Expression expressionRight;

    public Add(Expression expressionLeft, Expression expressionRight)
    {
        this.expressionLeft = expressionLeft;
        this.expressionRight = expressionRight;
    }

    @Override
    public Object evaluate(Map<String, Vector> map)
    {
        Vector left = (Vector) this.expressionLeft.evaluate(map);
        Vector right = (Vector) this.expressionRight.evaluate(map);

        return left.add(right);
    }
}

import java.util.Map;

public class Multiply implements Expression
{
    private Expression expressionLeft;

    private Expression expressionRight;

    public Multiply(Expression expressionLeft, Expression expressionRight)
    {
        this.expressionLeft = expressionLeft;
        this.expressionRight = expressionRight;
    }

    @Override
    public Object evaluate(Map<String, Vector> map)
    {
        Object left = expressionLeft.evaluate(map);
        Object right = expressionRight.evaluate(map);
        Vector vector;
        double value;

        if (left instanceof Vector)
        {
            vector = (Vector) left;
            value = (Double) right;
        }
        else
        {
            vector = (Vector) right;
            value = (Double) left;
        }

        return vector.multiply(value);
    }
}



2) How is expression built ?
Let's for example: (a+b)*4+2*c

Step by Step:

(1) Adding - a+b
(2) Multiplication - (1) i 4
(3) Multiplication - 2 i c
(4) Adding - (2) i (3)

So we can do this like the following, it is as we create tree:


import java.util.HashMap;

public class Client
{
    public static void main(String[] args)
    {
        Expression expression =
                new Add(
                        new Multiply(
                                new Add(
                                        new Variable("a"),
                                        new Variable("b")
                                ),
                                new Constant(4)
                        ),
                        new Multiply(
                                new Constant(2),
                                new Variable("c")
                        )
                );

        Vector v = (Vector) expression.evaluate(new HashMap<String, Vector>()
        {
            {
                put("a", new Vector(2, 3));
                put("b", new Vector(3, 5));
                put("c", new Vector(1, 2));

            }
        });

        System.out.println(v.x + " " + v.y);
    }
}