Think about Loose Coupling | |
PerlMonks |
Re: Re: Re: Re: Class::Interface -- isa() Considered Harmfulby chromatic (Archbishop) |
on Jan 19, 2003 at 21:37 UTC ( [id://228238]=note: print w/replies, xml ) | Need Help?? |
Inheritance does two completely different things in Perl. First, it expresses a conceptual relationship. This is the hierarchy you mention. If you need to model buildings, a library is a more specific type of building. So is a restaurant. Second, it provides a place to look for methods called on the object that aren't defined in the class itself. This is inheritance of behavior. There are plenty of other ways to make sure all of the methods are in place. You can define them all yourself. You can set up an AUTOLOAD scheme, as Test::MockObject does. You can use mixins. You can delegate to another object (with a proxy or a bridge). You can contain another object (with composition). I can take care of #2 by any of those other methods. That's what my mocks do. Why should I have to rely on inheritance to provide #1? I'd rather have a more general solution, one that allows me to say "an object of this type behaves as an object of this other type". If that's in place, inheritance can still work just fine -- derived objects are automatically marked as being able to act as objects of their parent classes. That's exactly what Class::Interface does. Can you give an example that shows where ISA wouldn't be appropriate? A Car object contains for Wheel objects. I have code that wants to find the circumference of all Wheels in my universe, calling get_circumference() to do so. It looks at all of the objects in my universe to see if they can handle the operations a Wheel can handle. Since Car does not inherit from Wheel, it can't check isa(). If Car is marked as having the same fingerprint as Wheel, it will work. I'm not interested in any solution that suggests "You should have an abstract base class for hasWheel or delegatesToWheel." I just want to be able to say "Car can handle messages to Wheel" without expressing a relationship that's not there or exposing how the Car handles Wheel messages.
In Section
Meditations
|
|