- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
More Evil Inheritance
Tight on the heels of my article on the evils of 'extends' and Scriptaculous, Bernard Sumption, the author of Animator.js, has expounded on why he thinks it is evil and must be destroyed.
All of the pain caused by inheritance can be traced back to the fact that inheritance forces 'is-a' rather than 'has-a' relationships. If class R2Unit extends Droid, then a R2Unit is-a Droid. If class Jedi contains an instance variable of type Lightsabre, then a Jedi has-a Lightsabre.
The difference between is-a and has-a relationships is well known and a fundamental part of OOAD, but what is less well known is that almost every is-a relationship would be better off re-articulated as a has-a relationship.
As I've already pointed out, composition is to be preferred over subclassing in most cases, and that the GoF patterns are all about replacing implementation inheritance (extends) with interface inheritance (implements). Combine this with Larman's GRASP patterns, and you see that much of the thrust of OOAD is about working around the limitations of "extends."
Technorati Tags: ajax, javascript, ooad, patterns
Topics: Best Practices, Javascript, Patterns
Comments: 2 so far
Leave a comment
About Pathfinder
Recent
- Thanksgiving 2008: What We’re Thankful For (In Rails)
- iPhone SDK: Testing with TextMate & GTM
- GWTQuery - JQuery-like Syntax in GWT
- Ask the readers: How do I fire native browser events in Prototype.js?
- News Rollup for the Week of November 17, 2008
- Rails ThreatDown!
- Automated Deployments Rock
- Bandwidth profiling Flex projects and more with Charles
- iPhone SDK: UIViewController Testing & TDD
- Icons are evil; so are menus - unless you do them right
Archives
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006


As far as I am concerned I must congratulate you both on having re-invented LSP about 30 years after it first saw daylight…
Nothing new here except that the author has completely overlooked MI…!
Imagine this;
class DarkPowers {}
class Android {}
class DardkAndroid : extends Android _&&_ DarkPowers {}
Fact is for everybody that’s done more than slightly advanced C++ MI solves most of the IS A/HAS A problems that Java and C# suffers from…
So does also JavaScript…
Look at this one for instance; http://smartwin.cvs.sourceforge.net/smartwin/SmartWin/include/smartwin/widgets/WidgetTextBox.h?revision=1.39&view=markup
The above class is an extremley beautiful example of using IS A in a Multiple Inheritance environment without ever even being CLOSE to breaking Mrs. Liskow Substitution Principle what-so-ever…!
I’m doing the exact same thing in JavaScript too in fact…!
So unless you want to type out forward methods to a gazillion methods using “HAS A” you can easily utilize MI to inherit from a truckload off classes…
Though from a single inheritance view I must confess I kind of agree…
MI though goes completely ballistic with your entire thesis…
.t
Comment by Thomas Hansen, Monday, October 1, 2007 @ 7:03 pm
Douglas Crockford wrote a witty post about .extends a couple of years back. Check it out here:
http://blog.360.yahoo.com/blog-TBPekxc1dLNy5DOloPfzVvFIVOWMB0li?p=440
Comment by Brian Dillard, Tuesday, October 2, 2007 @ 4:38 pm