The sprintabout is a join work between Rémi Forax Etienne Duris and Gilles Roussel.
The sprintabout is a really fast implementation of the visitor pattern concept.
It's a 100% pure Java library that add multi-dispatch (with a single parameter)
to Java without changing the compiler or the Java Virtual Machine.
The sprintabout makes dispatch code more readable, understandable and
less error prone by avoiding cascade of instanceof on argument type.
The semantics of the sprintabout is greatly inspired of its little cousin of the
runabout.
The sprintabout is freely available on license LGPL.
In case of multi-dispatch with more than one argument, you can use an older framework. Java MultiMethod Framework.
import fr.umlv.sprintabout.VisitorGenerator; //argument types hierarchy interface A {} class A0 implements A {} class A1 implements A {} class A2 implements A {} public abstract class SumSprintabout { public int value(A0 a) { return 0; } public int value(A1 a) { return 1; } public int value(A2 a) { return 2; } public abstract int valueAppropriate(A a); public static int sum(A[] array) { int sum = 0; VisitorGenerator vg = new VisitorGenerator(); SumSprintabout visitor = vg.createVisitor(SumSprintabout.class); for (int i=0; i<array.length; i++) { sum += visitor.valueAppropriate(array[i]); } return sum; } public static void main(String[] args) throws Exception { A[] array = {new A0(), new A1(), new A2() }; System.out.println(sum(array)); } }
The sprintabout is really, really fast.
In server mode (using c2 compiler), the sprintabout is faster
than the visitor pattern and even faster than a simple dispatch using
dedicated method.
In client mode, the sprintabout is 30% to 100% faster than the
runabout depending on the class hierachy.
Little comparison between the runabout and the sprintabout :
Runabout | Sprintabout | |
License | GPL | LGPL |
Inheritance | fixed | free |
Creation | free | fixed, need a default constructor |
Return type | no | yes |
Method: modifiers | public | all modifiers but private |
Method: static | no | yes |
ThreadSafe | yes | yes |
JDK compatibility | 1.2 | 1.5 |
Share Type states | newly type states are thread specific | all states between all threads |
Technique | generic code + dedicated method call | dedicated code (no generic part, avoid some cast) |
Current source of the sprintabout implementation :
sprintabout-src.zip
In order to work, the sprintabout relies on version 3.0 of ASM framework.
ASM is tiny an efficient bytecode manipulation framework.
You can download it here.
If you want more information, you can contact me by email.