Update: my phrasing below is rather vaugue as to my intent, I'm afraid. I'm not really asking about separating constructors and factories into separate methods or classes; I'm not asking whether 'new' or any other name is symantically acceptable for these constructs. I am asking about parameter-based subclass generation vs direct instantiation, regardless of how instances are constructed, and more generally about the propogation of defaults through subclasses. Apologies for my obtuseness. Thnx. -- Matt

--- original text below ---

Is there any particular reason why having a constructor serve a dual role as a factory is a bad idea? In other words, if you have a base class with some subclasses, you can either a) instantiate directly from the subclass, or b) have the base class hand you one of the sub classes depending on parameters.

Lately I have found myself wanting to do this very thing, in an attempt to maintain flexibility while at the same time having reasonable defaults, some of which percolate down to subclasses, some of which do not. In other words, if I know ahead of time that I need one of the subclasses, then instantiate directly from that subclass. If I don't care, then instantiate from the base class and let it do what it wants to do by default. Or, dynamically hand the base class a parameter once your program logic dictates which subclass you need (I realize there are any number of ways to load classes dynamically, but this way you can imbed that knowledge/capacity in the classes rather than in your program).

If this is perfectly acceptable, by what name do we call the technique? And in which schools of thought might it provoke howls of rage?

Example:

File Bubba.pm
package Bubba; use strict; use warnings; our %Defaults = ( type => 'Gump', ); sub new { my $this = shift; my $class = ref $this || $this; my %parms = @_; my $type = exists $parms{type} ? $parms{type} : $Defaults{type}; delete $parms{type}; if ($class eq __PACKAGE__) { # if subclasses were in separate modules, we'd # dynamically load here $class .= "::$type"; return $class->new(%parms); } my $self = {}; bless $self, $class; # do stuff with parms as the subclass $self; } sub shout { print "Unobtainable shrimp. No subclass?\n" } package Bubba::Gump; use base 'Bubba'; sub shout { print "Boiled shrimp\n" } package Bubba::Wump; use base 'Bubba'; sub shout { print "Fried shrimp\n" } 1;

File summon_bubba.pl
#!/usr/bin/perl -w use strict; use Bubba; my $b = Bubba->new(); $b->shout; my $g = Bubba->new(type => 'Wump'); $g->shout; my $bg = Bubba::Gump->new(); $bg->shout; my $bw = Bubba::Wump->new(); $bw->shout;

# ./summon_bubba.pl Boiled shrimp Fried shrimp Boiled shrimp Fried shrimp
Cheers,
Matt

In reply to Constructor/Factory Orthodoxy by mojotoad

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.