Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
There is also a school of thought (in which I'm enrolled {grin}) that an object should also be a "valid" object upon instantiation, and that a caller should never be given a partially constructed object that has to be prodded a bit before it would be valid for the rest of the program.

Indeed. I've learned the hard way that approaches to OO that leave objects in an "I'm almost valid, really!" state are either asking for trouble now, or are investing in future trouble for the poor soul who picks up the code a year later, tries to extend it, and doesn't get the partial initialization exactly right.

Clients of an API should never--at least never in production code--be handed invalid objects with the expectation that the client will do the right thing to get the object into a valid state.

At the very least, this means that initialization parameters passed to new need to be honored.

  sub new {
      my $proto = shift;
      bless { @_ }, ref($proto) || $proto;
This suffices to create instances of some simple objects, but often times setup is a bit more complicated. One pattern from the Smalltalk world is to separate initialization from instantiation, using a generic method for the latter, and a method for the former that subclasses can override.

Using the pattern, the base class implements new (instantiation) and initialize (initialization).

sub new { my $proto = shift; my $self = bless { @_ }, ref($proto) || $proto; $self->initialize(); } sub initialize { my $self = shift; ... initialization for the base class ... }
Subclasses then specialize the initialization.
sub initialize { my $self = shift; # First initialize our inherited aspects $self->SUPER::initialize(); ... initialization specific to the subclass ... }
When I then create an instance of a subclass in this hierarchy, I get the generic new, followed by a pre-order invocation of initialize. I.e., first the object is initialized as if it is a member of the base class, and the initializations for subclasses happen in the order of refinement until I get down to the class I targeted. I end up with a fully initialized object.

Using this scheme, at least with the class of objects for which this type of initialization works, I'm guaranteed to get a valid object back from new.

In reply to (dws)Re: Re: simple OO constructor by dws
in thread simple OO constructor by galacticdruid

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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2021-12-01 16:21 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (13 votes). Check out past polls.