Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The problem that you're running into is that you're trying to run inheritance the wrong way (and possibly for the wrong reasons).

The short answer is that you can't have a high-level class be aware of a low-level class' functions without embedding those kind of ugly stubs in your code (at least, not that I know of).

The longer answer is this:

If all subclasses of class Utils will have a method called do_something(), then having a method stub (meaning a method that does nothing) is appropriate.

sub do_something() { return 1; }

This would allow you to pass around objects belonging to any of Util's subclasses without worrying about their specific type. You would know that all Util subclasses could be made to perform some action simply by calling do_something(), whether or not the something to be done is the same for each subclass.

Looked at another way, if Util needs to be aware of a subclass' implementation of method then you've got the either the class hierarchy, or the class in which this method is declared, wrong. If calling Util::method() relies on Util::LowLevel::method(), then method() doesn't belong in the Util class since it doesn't work at that level of abstraction.

Does that make sense?

The other thing that is sounds like you are running into is the conflict between inheritance and composition.

You're trying to build a toolset to be offered by Util while relying on a LowLevel toolset supplied by a class that inherits from it. This sounds like a case for composition, not inheritance -- HASA vs. ISA. If your Util class needs a LowLevel toolset, then you might want to tackle it this way:

package Utils; sub new { my $class = shift; my @args = @_; my $self = {}; $self->{toolset} = new Toolset; return bless $self, $class } sub read() { my $self = shift; $self->{low_level_stuff} = new Functions::LowLevel; } sub doSomeOperation() { return $self-{low_level_stuff}->read(); } package Toolset sub new {} sub read {} package Toolset::LowLevel @Toolset::LowLevel::ISA = qw( Toolset ); sub new {} sub read { my $self = shift; return $self->readLine(); }

This approach probably only makes sense if you plan on having more than one toolset subclass (or you're doing a lot of OO work for little return). But now you have isolated Utils from the LowLevel Toolset class successfully and you could, for instance, make Toolset smart enough to instantiate itself differently (and return different low-level toolsets) based on the platform or something. This is one way to get a balance of abstraction and functionality, although there are, undoubtedly, many more


In reply to Re: Re: Re: Faking new() method in high-level package by jreades
in thread Faking new() method in high-level package by belden

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-16 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found