Imagine the following class exists:
public class Foo extends Spam{
public void bar(){};
//...many more useful functions...
}
I now need this:
public class Fooish extends Foo{
public static boolean whichDelegate; //true for 1, false for 2
Foo delegate1;
Foo delegate2;
}
Is there a way to automatically have java delegate the calls?
After looking over many other responses here on SO, I see three possible ways, none of which seem to work for my particular use case:
Just override everything and do the switching logic yourself
The problem with this is that Foo is a class that changes a lot. Doing this would mean that every time Foo is changed, Fooish must be changed also.
Even worse, if it weren't changed and function eggs() were added to Foo, Fooish would call eggs itself rather than on its delegates. The chaos...
-
I would love to be able to do this, but Foo is a concrete class and lifting its functionality into an interface seems impossible. I can't touch Spam.
-
Has significant performance impact and is hard for others to understand. Calls to the delegates probably won't be able to be found by eclipse, which will hurt maintainability. Seems like the best choice, but seems annoying for what should have a compile time solution.
I also found this but its in C#