I am trying to decide which is the better way to dispatch on a type in AspectJ. Suppose I am performing a computation on a tree with three kinds of nodes. I could then write a simple Java method:
private void computation(TreeNode node) {
if (node instanceof Node0) {
// Do stuff.
} else
if (node instanceof Node0) {
// Do stuff.
} else
if (node instanceof Node0) {
// Do stuff.
}
}
or
private void computation(TreeNode node) {
switch (node.kindNode()) {
case NODE0:
// Do stuff.
break;
case NODE1:
// Do stuff.
break;
case NODE2:
// Do stuff.
break;
}
}
or I could inject a method into each node type:
private void Node.computation() {
throw new UnsupportedOperationException(getClass() + ".computation()");
}
private void Node0.computation() {
// Do stuff.
}
private void Node1.computation() {
// Do stuff.
}
private void Node2.computation() {
// Do stuff.
}
Which method is preferable and why?