in reply to Real live usage of inheritance?
Take a look at HTML::TokeParser::Simple. That module inherits from HTML::TokeParser. It obeys the Liskov Substitution Principle, it truly extends the functionality and, I think, is a decent example of what inheritance can do (though it would be better if blessing the tokens had been pushed down to the parent classes).
One problem with inheritence is determining what should really be inherited. For example, I see a lot of inheritence with no purpose other than to alter the object's data (that should probably be an instance) rather than altering (or extending) the object's behavior.
Also, just because you can inherit from something doesn't mean you should. For example. one time I was creating a set of Web tests and inherited from WWW::Mechanize. Instead of an is-a relationship, I actually had a has-a relationship and I needed to rewrite my code to delegate method calls to a Mechanize object. While this was a good thing to do from a design standpoint, it was doubly important in Perl as it's so easy to accidentally step on the "private" methods of a class:
package Foo; sub new { my $self = bless {}, shift; $self->_init } sub _init { 'initialize data here' } package Bar; use base 'Foo'; sub _init { warn "We didn't initialize our data :(\n" } package main; my $thing = Bar->new;
Cheers,
Ovid
New address of my CGI Course.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Real live usage of inheritance?
by hardburn (Abbot) on Nov 05, 2003 at 19:03 UTC | |
by adrianh (Chancellor) on Nov 06, 2003 at 11:52 UTC |