When you first call new(), the database is searched to find out if the item exists. If it does, you receive a Product::Real. Otherwise, you receive a Product::Potential. This has the downside that the developer needs to test this in their code:

my $god = Product->new('religion'); if ( $god->exists ) { # Product::Real } else { # Product::Potential }

And no, Product::Real shouldn't call create() again, but since that method won't be defined in the base class, it's guaranteed to fail if called.

The benefit of this is that we use Perl's OO mechanisms to determine if we handle a method rather than this obvious, but clunky code:

sub create { my $self = shift; if ( $self->exists ) { $self->croak('already exists'); } # create the object }

Effectively, we're testing the type of the object from within the object and that's generally a design flaw. Separating the classes means that Perl does this for us.

However, this does mean that outside of the class, developers need to make at least one test of the object type (the 'exists' test) and this concerns me. I like to minimize procedural code as much as possible as it's a constant source of bugs.

Cheers,
Ovid

New address of my CGI Course.


In reply to Re^2: Reblessing yourself by Ovid
in thread Reblessing yourself by Ovid

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.