3

I was refactoring some java to use decorators. All of the decorators inherited from a class ThingDecorator, let's say. This consisted entirely of:

SomeType methodName(OtherType otherThing) {
    return decoratedObject.methodName(otherThing);
}

For maybe 20 methods of that form. This is like writing "this thing is a decorator" over and over again, which just drives me nuts as a DRY enthusiast. Every addition to the activities of this class needs an addition to the class[es] that form the basic un-decorated behavior and the decorator, but it's always just "this thing is still a decorator!" all over again. Is there any way to express once that an object is to delegate all non-overridden methods to some member field?

sqykly
  • 219

1 Answers1

2

No, unfortunately, there isn't any way to accomplish this.

Unless you use some magic, like this: michael.gr - Intertwine: Normalizing Interface Invocations (which though I have implemented for C#, not for Java yet.)

Mike Nakis
  • 32,803