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?
AbstractThing
that was the contract, aBasicThing implements AbstractThing
that was the default behavior of aThing
, aThingDecorator implements AbstractThing
whose only task was to have a reference to anAbstractThing
and delegate all calls to it, a plethora ofThingWithSomeStuff extends ThingDecorator
which provided only the concrete meaning of theThing
having someStuff
, and finally aThingToPointAt
for hiding the decoration from things that don't know they need it.BasicThing
fulfills the contract provided by the interface, the abstract base decorator ensures that any concrete decorator that doesn't fulfill the entire contract has a reference to a thing that can and uses it to do so without needing to understand the entire contract of aThing
when all you want is for it to have someStuff
.Stuff
that could affect aThing
and many different kinds ofThing
, and new kinds ofStuff
were being added with every revision. Before I got there, allStuff
had to be defined, considered, and managed throughout theThing
that it affected. It was already quite complicated, my way is actually a lot neater.