On Mon, Jun 25, 2001 at 12:47:41PM -0700, David Whipp wrote:
>> What's wrong with multiple inheritance?
> You have to create a whole load of extra classes whose only
> purpose is to define a list of superclasses. Compare:

This sounds a lot like the case I had with a
<buzzword type="cheesy">content-syndication</buzzword> system I'm
working on.
Basically, I wanted to create a Feed-object that inherits from any
Fetch-class and any Parse-class. Something like this:

bless $obj1, qw/Fetch::LWP Parse::HTML/;
bless $obj2, qw/Fetch::POP3 Parse::CSV/;

but then I realized that since the Fetch and Parse-classes never diddled
with $self, I didn't need inheritance at all. Delegation was more than
good enough in my case.

I ended up with something like this (simplified a bit):

package Feed;

sub new {
 my $class = shift;
 my %args = @_;
 my $f = $args{FETCH}->new;
 my $p = $args{PARSE}->new;
 bless {FETCH => $f, PARSE => $p} => $class;
}

sub fetch {
 my $self = shift;
 return $self->{FETCH}->fetch(@_);
}

sub parse {
 my $self = shift;
 return $self->{PARSE}->parse(@_);
}

sub cleanup {
 my $self = shift;
 $self->{FETCH}->cleanup(@_);
 $self->{PARSE}->cleanup(@_);
}

etc..


which works very well.

The downside is of course that I need to make a small stub for every
single function I want to delegate. Since it's only a few function I
need from the classes, it's not a big problem. If you have 50 functions
in each class, it could be a hassle.
I haven't given it much thought, but I imagine the cleanup-method could
be difficoult to implement with inheritance.

I don't know if delegation is the solution in your case though. And I
certainly can't claim that it is the solution in every "multibless"
case. But it worked for me :)

-- 
Trond Michelsen

Reply via email to