[Bioperl-l] multiple inheritance

Hilmar Lapp hlapp at gnf.org
Thu Jul 31 14:32:38 EDT 2003


On Thursday, July 31, 2003, at 12:48  AM, Ewan Birney wrote:

> Have a single holder object which multiply inheriets from each 
> interface
> and has-a copy of each object. Put code which knows about "both" 
> objects
> into the holder object and delegate interface calls to the right 
> object.
>
> This will actually keep your code cleaner
>

Not necessarily (although I generally agree with your statement).

SimilarityPair.pm does use double delegation, but at initialization 
time things get messy because some of the methods are overridden to 
delegate when the delegation object is not yet instantiated, so a 
default sub=feature object is instantiated.

The problem with this is when you inherit off SimilarityPair and want 
to install your own delegation implementors, this together with having 
the inherited constructor doing the initialization work gets 
non-trivial because at the time you get to initialize the delegation 
objects the object itself has been initialized already due to the chain 
of constructors, and hence there are already these partially populated 
default delegation objects from which you have to copy (and therefore 
know) properties now, aside from the fact that you wasted CPU cycles by 
creating objects that are thrown away a few milliseconds later (and for 
features that can have an impact as there may be tens of thousands of 
them). This caused a number of bugs in SimilarityPair and SearchIO 
classes that used it, which were hard to track down.

The solution right now is that FeaturePair (from which SimilariPair 
inherits) accepts a factory for creating the delegation objects if it 
needs them when they aren't there yet. Inheriting classes that need 
their own implementors push an appropriate factory onto the argument 
list before chaining to the inherited constructor. (BTW you could not 
do this in Java, but setting properties in bulk through the constructor 
is generally very uncommon in Java code that I've seen.)

You could probably do this with some sort of _init() chaining as well, 
but may point is that delegation doesn't necessarily protect you from 
some really nasty details.

	-hilmar
-- 
-------------------------------------------------------------
Hilmar Lapp                            email: lapp at gnf.org
GNF, San Diego, Ca. 92121              phone: +1-858-812-1757
-------------------------------------------------------------




More information about the Bioperl-l mailing list