4 ms·
Composition solves that problem, but I don't know about "nicely". You end up having to litter each class with a bunch of boilerplate methods which simply call t
by nradov 16y ago
Composition solves that problem, but I don't know about "nicely". You end up having to litter each class with a bunch of boilerplate methods which simply call the same method on an instance field. This avoids inheritance problems but you can end up with a lot of extra code.
What I would like to see — at least in Java — is a keyword or annotation for instance fields to mark them as pseudo-superclasses. Then the compiler would generate all of those call-through methods automatically, unless overridden in the current class.
- InclinedPlane 16y agoIt's called the strategy pattern. Look it up. If you find yourself writing a lot of boilerplate code, you're doing something wrong, whether you're using inheritance or not.
- nradov 16y agoYes, I've read that book too. For many of the cases I deal with the strategy pattern is not appropriate, or creates additional complications. Try again.
- InclinedPlane 16y agoShould we raise this discussion to the level of concrete examples? Pastebin perhaps?