Dan Lydick wrote:
Your example can as easily use class references as it can be written using method pointers.<>From: Danny Angus <[EMAIL PROTECTED]>
1/ Ok don't flame me... "Method pointers"
But still, be sure to watch for flames from the
Purist Society! They _do_ have a point, ya know.
I *know* it is possible to accomplish all the delegation one might want by
using polymorphism, but this often leads to unncessary screeds of
boiler-plate,
so I still I believe there is a case for some kind of streamlining of
delegation by allowing it to be achieved on a method level rather than at
class level.
Where's the advantage of a method pointer?
class ImplementsComparable {
interface ComparableMethod {
void compare (Comparable c) { ... }
}
class CompareMethodImpl1 implements ComparableMethod {
void compare (Comparable c) { ... }
}
class CompareMethodImpl2 implements ComparableMethod {
void compare (Comparable c) { ... }
}
class CompareMethodImpl3 implements ComparableMethod {
void compare (Comparable c) { ... }
}
ComparableMethod compareMethodImpl1 = new CompareMethodImpl1 ();
compareMethodImpl2 = new CompareMethodImpl2 ();
compareMethodImpl3 = new CompareMethodImpl3 ();
}
sampleCompareMethod (ImplementsComparable comparableObject, Integer compareWith) { ComparableMethod compareMethodImpl;
switch (compareMode) { case 1: compareMethodImpl = comparableObject.compareMethodImpl1; case 2: compareMethodImpl = comparableObject.compareMethodImpl2; case 3: compareMethodImpl = comparableObject.compareMethodImpl3; } return compareMethodImpl.compare (compareWith); }
If you are desperate to use method pointers you can use a method pointer in Java
using java.lang.reflect.Method nearly the way you suggested it to be used:
public final static Method COMPARE_METHOD_1 = ImplementsCompare.class.getMethod ("compareMethod1", parameterTypes||);
public final static Method COMPARE_METHOD_2 = ImplementsCompare.class.getMethod ("compareMethod2", parameterTypes||);
public final static Method COMPARE_METHOD_3 = ImplementsCompare.class.getMethod ("compareMethod3", parameterTypes);||
switch (compareMode) { case 1: compareMethod = COMPARE_METHOD_1; case 2: compareMethod = COMPARE_METHOD_2; case 3: compareMethod = COMPARE_METHOD_3; } return compareMethod.invoke (comparableObject, parameters);
If the parameters have different names it's likely they have different types so you would probably want to invoke methods with the exact same parameter list, which defeats your argument that not knowing parameter names is an advantage.
The main difference is that in the earlier piece of code methods are wrapped in inner classes, which amounts to about 30 additional characters you have to type: "class FooBar { ... } implements I" for each class, one instantiation for each class and a common interface.
--
www.citizens-initiative.org <http://www.citizens-initiative.org/>