Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
A Refactoring for Perl.

You need to subclass a class that contains factory methods,
and also the class that the factory methods return.


sub factory { my $self = shift; ... my $newInstance = new Bar(...); ... return $newInstance; }
sub factory { my $self = shift; ... my newInstance = $self->factoryClass()->new(...); ... return newInstance; } sub factoryClass { "Bar" }


Factory methods return instances of some class known to the factory method. Typically the class name is hard-coded. If the class that holds the factory method is subclassed, the factory method is inherited, but will continue to return instances of the same hard-coded class. This isn't always desirable. A set of cooperating base classes, one of which uses a factory method to create instances of the other, may both need to be subclassed. If a factory method uses a hard-coded class name, that factory method must be copied into a subclass and modified. If the factory method performs other work (e.g., bookkeeping to account for the newly created instance), this can result in redundant code in the class hierarchy.

The solution is to "soft code" the class name that the factory method will create new instances of, by creating a new method that returns the class name, and using the new method in place of the hard-coded class name. The factory method can then be inherited by subclasses, which merely need to override the method that returns the class name.


  • Identify a factory method that hard-codes a class name.
  • Create a new method that returns the class name.
  • In the factory class, replace the hard-coded class name with an invocation of the new method.
  • Test.


I first ran into the need for this when attempting to subclass pieces of GIFGraph, and finding myself initially thwarted by hard-coded class names in its factory methods. (I had to refactor methods to create some factory methods, but that's a separate story.)

This refactoring is hardly original thinking. It's been done in the Smalltalk world for years. If you know of an existing writeup of this refactoring for Perl, please provide a reference.

In reply to Refactoring: Soft-code class name in factory method by dws

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [stevieb]: LOL you can easily replace "Helsinki" with "Alberta"
    [perldigious]: I used to live at the top of a very steep hill stevieb, I can relate to the video. Literally used to have to build up momentum and try to force my vehicle to make it up the hill to park in my driveway properly... didn't always make it.
    [choroba]: Why don't they use tire chains?
    [perldigious]: First snow, probably not prepared right away choroba.
    [stevieb]: it's Quebec... they do weird stuff there :) In the mountains, it is *mandatory* for trucks to use tire chains. Many passenger vehicles do as well (but it's not mandatory for them)
    [perldigious]: That stuff can hit fast... like what we just got where I'm at. Saw a few people who had slid OUT of the roundabout I have to drive through to get to work, not used to the ice yet, take awhile to get the hang of it again and make vehicle adjustments.
    [stevieb]: man, when the roads are glare ice, I don't even bother going to work or out... unless I absolutely have to, or was already out in the first place
    [choroba]: Tire chains are mandatory here in mountains, and the only unprepared each year are gritters
    [stevieb]: I have a 3 day winter survival kit in my vehicle in the event I get snowed in in the mountains (which has happened before due to avalanches closing the roads (and once in the summer due to a massive forest fire that trapped us
    [perldigious]: a handful of people in my work area did not make it, but I live pretty close and it's flat the whole way, so I didn't have any trouble. The roundabout is the worst thing I have to negotiate.

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (9)
    As of 2016-12-06 16:36 GMT
    Find Nodes?
      Voting Booth?
      On a regular basis, I'm most likely to spy upon:

      Results (112 votes). Check out past polls.