the only difference between each of the do_something methods are a couple of constants that I already know at compile time.

It matters who knows these constants. If these are only known in the "final" classes, then you can use a single do_something() inherited from the parent class, and then arrange for each of the specializing classes to provide methods to return the constants.

Let's consider a simple "B inherits from A" case.

package A; sub new { my $pkg = shift; bless {}, $pkg; } sub do_something { my $self = shift; my $n = $self->x() + $self->y(); print "something is $n\n"; } sub x { die "subclass must override x\n" } sub y { die "subclass must override y\n" } package B; @ISA = (A); sub x { 42 } sub y { 47 } package main; my $object = new B(); $object->do_something; # A::do_something invokes B::x and B::y
This adapts easily to multiple levels of inheritance. For example, the first level of inheritance can override x() and the next level can override y().

An alternative is to stuff the constants into the object's hash at initialization time.

package A; sub new { my $pkg = shift; my $self = bless {}, $pkg; $self->init(); } sub init { die "subclass must override init()\n" } sub do_something { my $self = shift; my $n = $self->{x} + $self->{y}; print "something is $n\n"; } package B; @ISA = (A); sub init { my $self = shift; $self->{x} = 42; $self->{y} = 47; } package main; my $object = new B(); $object->do_something(); # A::do_something reaches into the bag, and pulls out # numbers that B::init provided
In both cases, you could provide default behavior in the superclass.


In reply to Re: Re: Re: Trying to re-use code at different levels of an inherited object. by dws
in thread Trying to re-use code at different levels of an inherited object. by ehdonhon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.