"delegation vs inheritance"
Nov. 8th, 2007 11:11 amA case from my practice.
We have a class, BaseComponent, which takes an instance of ServiceProvider, our universal delegate, which does all the job we don't know how to do:
Now we have a bunch of subclasses, like DocumentComponent, ImageCompoent, SoundComponent, OlfactoryComponent, EroticComponent, etc; all of them use the service for various, but more or less limited number of purposes. And everyone writes stuff like this:
Now, wtf, I say, it is like assembler. Let's introduce methods!
etc.
And you know what? People start complaining! They say that "inheritance has no advantage over delegation"! Wtf! Where do they see inheritance vs delegation? It is encapsulation vs assembler language programming style! But I can't figure out where to start telling them the difference between encapsulation and inheritance!
What do you think? Is it sane? Am I wrong?
We have a class, BaseComponent, which takes an instance of ServiceProvider, our universal delegate, which does all the job we don't know how to do:
class BaseComponent {
ServiceProvider service;
public BaseComponent(ServiceProvider service) {
this.service = service:
}
protected ServiceProvider getService() {
return service;
}
}
Now we have a bunch of subclasses, like DocumentComponent, ImageCompoent, SoundComponent, OlfactoryComponent, EroticComponent, etc; all of them use the service for various, but more or less limited number of purposes. And everyone writes stuff like this:
...
String name = getService().getUserName();
Orientation orientation = getService().getUserOrientation();
List languages = getService().getUserLanguages(); (etc
...
Now, wtf, I say, it is like assembler. Let's introduce methods!
class BaseComponent {
....
protected String getUserName() {
return service.getUserName();
}
protected Orientation getUserOrientation() {
return service.getUserOrientation();
}
...
etc.
And you know what? People start complaining! They say that "inheritance has no advantage over delegation"! Wtf! Where do they see inheritance vs delegation? It is encapsulation vs assembler language programming style! But I can't figure out where to start telling them the difference between encapsulation and inheritance!
What do you think? Is it sane? Am I wrong?