Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Refactoring: Soft-code class name in factory method

by dws (Chancellor)
on Apr 24, 2001 at 04:39 UTC ( #74924=perlmeditation: 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.

Change

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

Motivation

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.

Mechanics

  • 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.

Discussion

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.

Comment on Refactoring: Soft-code class name in factory method
Select or Download Code
Re: Refactoring: Soft-code class name in factory method
by merlyn (Sage) on Apr 24, 2001 at 05:33 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://74924]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-10-23 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (126 votes), past polls