Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This sounds like the question of should i inherit from a base class, or should i include an instance of some other class in my class. What i mean is, if you have a calendar class, and you want calendar functionality in a basically unrelated class, such as some specialized DB class, you probably dont want to have the DB class inherit from the Calendar class, you probably want an instance of the Calendar class in the DB, which then has a well defined interface.
package MyDBClass; use MyCalendar; sub new { my $class = shift; ... my $self = ...; ... $self->{'_calendar'} = MyCalendar->new(); ... return bless $self, $class; } sub Calendar { return $self->{'_calendar'}; } ...
Then you have a calendar as a data element of your DB class, and you can do things like:
my $db = MyDBClass->new(); $db->Calendar()->CalendarFunction(...);
Now there are other, more sophisticated ways to deal with the calendar in the DB, but this should illustrate the point.

The moral is, if a class wants some other packages functionality, it doesnt have to be a child class, it can just use the other class.


In reply to Re^3: OO, inheriting functions from other packages by shemp
in thread OO, inheriting functions from other packages by sschneid

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 perusing the Monastery: (7)
As of 2024-04-23 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found