It would be very helpful if Java had a syntax to explicitly specify delegation, something in the lines of:
public class SomeClass extends SomeSuperClass, delegates Interface in interfaceImp{ InterfaceImp interfaceImp; ... }-- EduardoMadrid
or
public class SomeClass extends SomeSuperClass, implements Interface { delegate Interface to InterfaceImp interfaceImp; ... }-- RafaelAlvarez
This has been implemented as a native Java API, though it's still in a proof-of-concept phase. It's called Delegator and can be found on http://sourceforge.net/projects/delegator/
I've currently (begin 2005) started development again as a masters thesis so more should be available in the coming months.
See DelegatorIsDelegationInJava
If you use a good IDE like IntellijIdea, it will take a lot of the burden off your shoulders. For instance, IDEA has a feature called Delegate Methods.
But, as much as I love IntellijIdea, it would be useful not to clutter the source code with the delegation functions
Shouldn't this be required too?
public class SomeClass extends SomeSuperClass, implements Interface { delegate Interface to InterfaceImp interfaceImp; public SomeClass() { interfaceImp = new InterfaceImp(); } ... }because I understand that the delegate keyword is meant to be a short hand for
public class SomeClass extends SomeSuperClass, implements Interface { private InterfaceImp interfaceImp; public SomeClass() { interfaceImp = new InterfaceImp(); } public ReturnType interfaceMethod1(Object param1) { return interfaceImp.interfaceMethod1(param1); } ... }So the interfaceImp has to be instantiated first.
I suppose the case for method clash can be resolved as multiple inheritance in C++, i.e. clashed methods have to be explicitly declared.
Note that Java doesn't provide a way to resolve collisions between methods from different interfaces. I find this disturbing. If methods have the same name they get the same implementation, no matter what context (interface) they are declared in.
Many years ago I thought this was a good idea. Since then I find myself rarely delegating behavior. I'm not exactly sure why. Perhaps it's a side effect of more refactoring. -- EricHodges
I'm a big fan of delegation, and concepts like DelegationInheritance; which is why AutomaticDelegation? is one of the ideas I'd like to have implemented in DefnLanguage (i.e. MyFavouriteNonexistantProgrammingLanguage?). -- KarlKnechtel
Automated Delegation is a Viable Alternative to Multiple Inheritance in Class Based Languages:
http://www.cs.virginia.edu/~evans/cs655-S00/readings/viega.pdf