Skip to content

Instantly share code, notes, and snippets.

@fffej
Created July 12, 2012 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fffej/3097258 to your computer and use it in GitHub Desktop.
Save fffej/3097258 to your computer and use it in GitHub Desktop.
Expressions
public interface Expression {
int evaluate();
}
public class Constant implements Expression {
private final int value;
Constant(int value) { this.value = value; }
public int evaluate() { return value; }
}
public abstract class BinaryExpression implements Expression {
private final Expression lhs;
private final Expression rhs;
BinaryExpression(Expression lhs, Expression rhs) {
this.lhs = lhs;
this.rhs = rhs;
}
public int evaluate() {
return op(lhs.evaluate(),rhs.evaluate());
}
protected abstract int op(int lhs, int rhs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment