|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Real live usage of inheritance?
by Ovid (Cardinal) on Nov 05, 2003 at 18:07 UTC | |
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:
Cheers, New address of my CGI Course. | [reply] [d/l] |
by hardburn (Abbot) on Nov 05, 2003 at 19:03 UTC | |
it was doubly important in Perl as it's so easy to accidentally step on the "private" methods of a class: In the case you present, I would argue that if the base methods are truly private, they should have been implemented as lexically scoped closures (declare my $init = sub { }; at the top of the package), not as a regular subroutine. It'd be nice if we could do away with the _subroutine_name idiom all together, but Perl doesn't currently provide a way to do protected methods (except by doing some fooling around with caller and isa, and even then it's only a runtime error), so the leading-underscore trick is the next best option. Since we have a trick for doing truly private functions (with closures), I consider any class that implements private method with a leading underscore to be broken. If it's a protected method, it should be documented for the benifit of subclasses (away from the public interface documentation, please). ---- : () { :|:& };: Note: All code is untested, unless otherwise stated | [reply] [d/l] [select] |
by adrianh (Chancellor) on Nov 06, 2003 at 11:52 UTC | |
I would argue that if the base methods are truly private, they should have been implemented as lexically scoped closures (declare my $init = sub { }; at the top of the package), not as a regular subroutine. (Niggle: Making a subroutine lexically scoped doesn't mean it is a closure.) Since most of the time all you want with a private method is to prevent subclasses breaking because they override it, there are a couple of other ways of protecting yourself without using anonymous subroutines.
It'd be nice if we could do away with the _subroutine_name idiom all together, but Perl doesn't currently provide a way to do protected methods (except by doing some fooling around with caller and isa, and even then it's only a runtime error), so the leading-underscore trick is the next best option. I've never come across people using the _method_name convention to indicate protected methods. Since most people use it to indicate private/implementation subroutines I'd probably consider it bad style myself. However, since I tend to find protected methods to be a bit of a code smell anyway I don't really care :-) | [reply] [d/l] [select] |
|
Re: Real live usage of inheritance?
by broquaint (Abbot) on Nov 05, 2003 at 18:02 UTC | |
So my question is, when/what have you actually used inheritance (for) in a 'real' project?Indeed, many times. I've used it to determine standard data access behaviour across classes, to inherit from a base object defintion and generally adding structure to classes (e.g fielding out utility methods to a utility module so as to not clutter up the basic class definition). Perhaps the most obvious use is laziness - why bother maintaining X methods across Y classes, when you can just have your subclasses inherit from your main class and maintain the non-overriden methods there (and as you can probably guess, laziness isn't the only benefit).
HTH
_________ | [reply] |
by hardburn (Abbot) on Nov 05, 2003 at 20:28 UTC | |
Perhaps the most obvious use is laziness - why bother maintaining X methods across Y classes, when you can just have your subclasses inherit from your main class and maintain the non-overriden methods there . . . Eek! Such is really false-laziness, as it tends to break object relationships into whatever the programmer felt like at the time, instad of a logical flow. It tends to make things a lot harder over time, since two classes that really have nothing to do with each other are suddenly in a parent-child relationship, just because there was a bit of code in the parent that was also needed in the child. Down that path lies many a broken OO project. ---- : () { :|:& };: Note: All code is untested, unless otherwise stated | [reply] [d/l] |
by broquaint (Abbot) on Nov 06, 2003 at 00:05 UTC | |
Eek! Such is really false-laziness ... since two classes that really have nothing to do with each other are suddenly in a parent-child relationshipEr ... perhaps. But I meant to imply without inheritence, it's rather more difficult to maintain related modules, not to inherit just because of a common method. So in conclusion, inheritence is powerful, but has its dangers, like most useful programming constructs.
HTH
_________ | [reply] |
|
Re: Real live usage of inheritance?
by Abigail-II (Bishop) on Nov 05, 2003 at 17:51 UTC | |
Abigail | [reply] |
|
Re: Real live usage of inheritance?
by gjb (Vicar) on Nov 05, 2003 at 18:08 UTC | |
I use inheritance quite a lot in real projects. One particular example in the old days was a framework for serialization/deserialization of objects in XML. This was implemented in both Perl and Java so that Perl objects could be serialized and deserialized as Java objects and vice versa. In both Perl and Java classes that implement this were subclasses of the 'GObject' class that had all the necessary logic. Another example: a library for finite state machines that also implements a finite state transducer. The latter adds functionality to the former, so it inherits all from the finite state automaton, but adds code to actually output things rather than just accepting or rejecting. Another example was a Perl wrapper around a C library that implemented connectivity to custom servers. All servers shared some functionality, but differed in the kind of requests one could do and the data they returned. Hence I had a base class implementing basic connectivity while the classes that inherited from it implemented the specific functionality. Oh well, very useful concept... just my 2 cents, -gjb- | [reply] |
|
Re: Real live usage of inheritance?
by hardburn (Abbot) on Nov 05, 2003 at 18:55 UTC | |
Class::DBI and CGI::Application make heavy use of inheritance, as does Exporter, though I wouldn't really call that OO--it's more like using Perl's OO-ish features in a non-OO way. Right now, I'm in the middle of a project that needs to generate reports based on certain data. The reports could come out in HTML, PDF, Excel, or even a CSV for exporting to another system. I've worked this by having an ExportFormat class which specific report formats inheirt from. After the constructor is called, a record() method is called many times with the argument as a single row from the database (actually, a Class::DBI subclass containing a single row). When the application is done filling the data, it calls a done() method, which returns a filehandle that contains the completed report. Since this is a web application, each report also has a mime_type() method that gives the MIME type that should be sent back to the client for this report. I find this solution to be quite elegant. ---- : () { :|:& };: Note: All code is untested, unless otherwise stated | [reply] [d/l] [select] |
by zby (Vicar) on Nov 06, 2003 at 09:53 UTC | |
| [reply] [d/l] |
|
Re: Real live usage of inheritance?
by tsee (Curate) on Nov 05, 2003 at 21:05 UTC | |
Hope that's enough examples of inheritance in "real" projects. Steffen | [reply] |
|
Re: Real live usage of inheritance?
by perrin (Chancellor) on Nov 05, 2003 at 18:09 UTC | |
| [reply] |
|
Re: Real live usage of inheritance?
by tilly (Archbishop) on Nov 05, 2003 at 19:47 UTC | |
If you want a real-world, understandable example using inheritance to mull over, grab libwww (that is LWP and friends) and study how that code works. | [reply] |
|
Re: Real live usage of inheritance?
by castaway (Parson) on Nov 05, 2003 at 22:14 UTC | |
I created a telnet client in perl, to which modules (plugins) can be added. The basic plugin has default values/methods for everything, each of the actual plugins inherits from this, and implements only the bits it actually needs. (Eg: The Alias module only implements the hooks to the user input methods, whereas the Trigger module needs the server input only). (Hmm, this'll probably only make sense to anyone who has played MUD or similar, whatever, you get the idea :) C. | [reply] |
|
Re: Real live usage of inheritance?
by Anonymous Monk on Nov 05, 2003 at 22:27 UTC | |
Another good example of inheritance used in "real life" is the Bioperl open source project. There is, for instance, a Bio::Seq object, which encodes a general sequence. A variety of other types of sequence inherit off this general one. That project also makes use of a lot of interface and factory classes (which I've never really understood well, so anyone who can explain will earn my ever-lasting gratitude). For example, a current discussion in the project is the development of wrapper code for the BLAST series of software programs. One idea is to generate a BLASTI (I = interface) class, along with accessory codes for all the others. In that way, anyone using a BLAST tool has a core standard interface to use & learn. | [reply] |
|
Re: Real live usage of inheritance?
by pjf (Curate) on Nov 06, 2003 at 10:12 UTC | |
Paul Fenwick Perl Training Australia | [reply] |
|
Re: Real live usage of inheritance?
by adrianh (Chancellor) on Nov 06, 2003 at 11:04 UTC | |
I constantly hear about inheritance being a prime factor in determining if something is OO or not, and how useful it is for OO programming. To some extent, inheritance is overemphasised by some people when talking about OO development. Not that it's completely useless, but you often find it used where options like delegation would be a better design choice. So my question is, when/what have you actually used inheritance (for) in a 'real' project? Real in this case meaning a project not done solely for experimentation purposes Some of my recent (real world) uses of inheritance have included: Basically, places where you want to specialise behaviour. | [reply] |
|
Re: Real live usage of inheritance?
by etcshadow (Priest) on Nov 06, 2003 at 19:44 UTC | |
"A BCBS-MA HMO Blue claim ISA BCBS-MA claim ISA BCBS claim ISA claim." Details get complicated and uninteresting from there, and that isn't quite an *exactly* correct example, but it gets the idea accross. There are business rules that apply specifically to BCBS-MA HMO Blue that make it behave differently than, say, BCBS-MA PPO. There are business rules that apply to all BCBS-MA plans that aren't common to other state/region BCBS's. There's also general stuff that all BCBS's have in common, but which are different when compared to large comercial insurance carriers like Aetna and Cigna, and likewise different from government carriers like Medicare, Medicaid and Champus (veterans). However, there are some things that are really the same between all of these. ------------ :Wq Not an editor command: Wq | [reply] [d/l] [select] |
|
Re: Real live usage of inheritance?
by submersible_toaster (Chaplain) on Nov 10, 2003 at 08:30 UTC | |
like in the wild? :) Abstract rate counters, have been spotted nesting with children who delight in measuring and playing with a variety of data such as; The children are shallow creatures , exhibiting a megre ammount of self. Pretty gross perhaps, but very lazy since these are usually constant things. I can't believe it's not psellchecked | [reply] [d/l] |