devxlogo

Templates in java

Templates in java

Question:
Any suggestions on a good way to implement templates in java?

Answer:
The normal way to implement template behaviorin Java is to create an interface defining thecommon behavior that is being abstracted. Everyclass that would normally would be instantiatedas a template in C++, would have to implementthis interface in Java. A good example of thisis the java.lang.Comparable interface in JDK 1.2.You can write a single sort method to sortany object implementing the Comparable interface,rather than writing N different sorts. However,you will find that this approach is not sufficientto achieve all of the benefits of C++ templates.You may consider doing a search on the web foran experimental Java compiler called Pizza, whichimplements extra functionality like templates.

devx-admin

Share the Post: